Debian Codegraph @

Tk/Table.pm

raw
1 # Copyright (c) 1995-2003 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 package Tk::Table;
5 use strict;
6
7 use vars qw($VERSION);
8 $VERSION = '4.016';
9
10 use AutoLoader;
11 use base qw(Tk::Frame);
12
13 Construct Tk::Widget 'Table';
14
15 # Constants for QueueLayout flags
16 sub _SlaveSize () { 1 } # Slave has asked for change of width or height
17 sub _SlaveChange () { 2 } # We lost or gained a slave
18 sub _ViewChange () { 4 } # xview or yview called
19 sub _ConfigEvent () { 8 } # Table has changed size
20 sub _ScrollBars () { 32 } # Scrollabrs came or went
21 sub _RowColCount () { 16 } # rows or columns configured
22
23
24 sub ClassInit
25 {
26 my ($class,$mw) = @_;
27 $mw->bind($class,'<Configure>',['QueueLayout',_ConfigEvent]);
28 $mw->bind($class,'<FocusIn>', 'NoOp');
29 $mw->XYscrollBind($class);
30 return $class;
31 }
32
33 sub _view
34 {
35 my ($t,$s,$page,$a,$op,$num,$type) = @_;
36 if ($op eq 'moveto')
37 {
38 $$s = int(@$a*$num);
39 }
40 else
41 {
42 $num *= ($page/2) if ($type eq 'pages');
43 $$s += $num;
44 }
45 $$s = 0 if ($$s < 0);
46 $t->QueueLayout(_ViewChange);
47 }
48
49 sub xview
50 {
51 my $t = shift;
52 $t->_view(\$t->{Left},$t->cget('-columns'),$t->{Width},@_);
53 }
54
55 sub yview
56 {
57 my $t = shift;
58 $t->_view(\$t->{Top},$t->cget('-rows'),$t->{Height},@_);
59 }
60
61 sub FocusChildren
62 {
63 my $t = shift;
64 return () if ($t->cget('-takefocus'));
65 return $t->SUPER::FocusChildren;
66 }
67
68 sub Populate
69 {
70 my ($t,$args) = @_;
71 $t->SUPER::Populate($args);
72 $t->ConfigSpecs('-scrollbars' => [METHOD => 'scrollbars','Scrollbars','nw'],
73 '-takefocus' => [SELF => 'takeFocus','TakeFocus',1],
74 '-rows' => [METHOD => 'rows','Rows',10],
75 '-fixedrows' => [METHOD => 'fixedRows','FixedRows',0],
76 '-columns' => [METHOD => 'columns','Columns',10],
77 '-fixedcolumns' => [METHOD => 'fixedColumn','FixedColumns',0],
78 '-highlightthickness' => [SELF => 'highlightThickness','HighlightThickness',2]
79 );
80 $t->_init;
81 }
82
83 sub sizeN
84 {
85 my ($n,$a) = @_;
86 my $max = 0;
87 my $i = 0;
88 my $sum = 0;
89 while ($i < @$a && $i < $n)
90 {
91 my $n = $a->[$i++];
92 $a->[$i-1] = $n = 0 unless (defined $n);
93 $sum += $n;
94 }
95 $max = $sum if ($sum > $max);
96 while ($i < @$a)
97 {
98 $sum = $sum-$a->[$i-$n]+$a->[$i];
99 $max = $sum if ($sum > $max);
100 $i++;
101 }
102 return $max;
103 }
104
105 sub total
106 {
107 my ($a) = @_;
108 my $total = 0;
109 my $x;
110 foreach $x (@{$a})
111 {
112 $total += $x;
113 }
114 return $total;
115 }
116
117 sub constrain
118 {
119 my ($sb,$a,$pixels,$fixed) = @_;
120 my $n = $$sb+$fixed;
121 my $total = 0;
122 my $i;
123 $n = @$a if ($n > @$a);
124 $n = $fixed if ($n < $fixed);
125 for ($i= 0; $i < $fixed; $i++)
126 {
127 (defined($a->[$i])) && ($total += $a->[$i]);
128 }
129 for ($i=$n; $total < $pixels && $i < @$a; $i++)
130 {
131 $a->[$i] ||= 0;
132 $total += $a->[$i];
133 }
134 while ($n > $fixed)
135 {
136 if (($total += $a->[--$n]) > $pixels)
137 {
138 $n++;
139 last;
140 }
141 }
142 $$sb = $n-$fixed;
143 }
144
145 sub Layout
146 {
147 my ($t) = @_;
148 return unless Tk::Exists($t);
149 my $rows = @{$t->{Row}};
150 my $bw = $t->cget(-highlightthickness);
151 my $frows = $t->cget(-fixedrows);
152 my $fcols = $t->cget(-fixedcolumns);
153 my $sb = $t->cget(-scrollbars);
154 my $H = $t->Height;
155 my $W = $t->Width;
156 my $tadj = $bw;
157 my $badj = $bw;
158 my $ladj = $bw;
159 my $radj = $bw;
160 my @xs = ($W,0,0,0);
161 my @ys = (0,$H,0,0);
162 my $xsb;
163 my $ysb;
164
165 my $why = $t->{LayoutPending};
166 $t->{LayoutPending} = 0;
167
168 if ($sb =~ /[ns]/)
169 {
170 $t->{xsb} = $t->Scrollbar(-orient => 'horizontal', -command => ['xview' => $t]) unless (defined $t->{xsb});
171 $xsb = $t->{xsb};
172 $t->Advertise('xscrollbar' => $xsb);
173 $xs[3] = $xsb->ReqHeight;
174 if ($sb =~ /n/)
175 {
176 $xs[1] = $tadj;
177 $tadj += $xs[3];
178 }
179 else
180 {
181 $badj += $xs[3];
182 $xs[1] = $H-$badj;
183 }
184 }
185 else
186 {
187 $t->{xsb}->UnmapWindow if (defined $t->{xsb});
188 }
189
190 if ($sb =~ /[ew]/)
191 {
192 $t->{ysb} = $t->Scrollbar(-orient => 'vertical', -command => ['yview' => $t]) unless (defined $t->{ysb});
193 $ysb = $t->{ysb};
194 $t->Advertise('yscrollbar' => $ysb);
195 $ys[2] = $ysb->ReqWidth;
196 if ($sb =~ /w/)
197 {
198 $ys[0] = $ladj;
199 $ladj += $ys[2];
200 }
201 else
202 {
203 $radj += $ys[2];
204 $ys[0] = $W-$radj;
205 }
206 }
207 else
208 {
209 $t->{ysb}->UnmapWindow if (defined $t->{ysb});
210 }
211
212 constrain(\$t->{Top}, $t->{Height},$H-($tadj+$badj),$frows);
213 constrain(\$t->{Left},$t->{Width}, $W-($ladj+$radj),$fcols);
214
215 my $top = $t->{Top}+$frows;
216 my $left = $t->{Left}+$fcols;
217
218 if ($why & (_ScrollBars|_RowColCount|_SlaveSize))
219 {
220 # Width and/or Height of element or
221 # number of rows and/or columns or
222 # scrollbar presence has changed
223 my $w = sizeN($t->cget('-columns'),$t->{Width})+$radj+$ladj;
224 my $h = sizeN($t->cget('-rows'),$t->{Height})+$tadj+$badj;
225 $t->GeometryRequest($w,$h);
226 }
227
228 if ($rows)
229 {
230 my $cols = @{$t->{Width}};
231 my $yhwm = $top-$frows;
232 my $xhwm = $left-$fcols;
233 my $y = $tadj;
234 my $r;
235 for ($r = 0; $r < $rows; $r++)
236 {
237 my $h = $t->{Height}[$r];
238 next unless defined $h;
239 if (($r < $top && $r >= $frows) || ($y+$h > $H-$badj))
240 {
241 if (defined $t->{Row}[$r])
242 {
243 my $c;
244 for ($c = 0; $c < @{$t->{Row}[$r]}; $c++)
245 {
246 my $s = $t->{Row}[$r][$c];
247 if (defined $s)
248 {
249 $s->UnmapWindow;
250 if ($why & 1)
251 {
252 my $w = $t->{Width}[$c];
253 $s->ResizeWindow($w,$h);
254 }
255 }
256 }
257 }
258 }
259 else
260 {
261 my $hwm = $left-$fcols;
262 my $sh = 0;
263 my $x = $ladj;
264 my $c;
265 $ys[1] = $y if ($y < $ys[1] && $r >= $frows);
266 for ($c = 0; $c <$cols; $c++)
267 {
268 my $s = $t->{Row}[$r][$c];
269 my $w = $t->{Width}[$c];
270 if (($c < $left && $c >= $fcols) || ($x+$w > $W-$radj) )
271 {
272 if (defined $s)
273 {
274 $s->UnmapWindow;
275 $s->ResizeWindow($w,$h) if ($why & 1);
276 }
277 }
278 else
279 {
280 $xs[0] = $x if ($x < $xs[0] && $c >= $fcols);
281 if (defined $s)
282 {
283 if ($why & 1)
284 {
285 $s->MoveResizeWindow($x,$y,$w,$h);
286 }
287 else
288 {
289 $s->MoveWindow($x,$y);
290 }
291 $s->MapWindow;
292 }
293 $x += $w;
294 if ($c >= $fcols)
295 {
296 $hwm++;
297 $sh += $w
298 }
299 }
300 }
301 $xhwm = $hwm if ($hwm > $xhwm);
302 $xs[2] = $sh if ($sh > $xs[2]);
303 $y += $h;
304 if ($r >= $frows)
305 {
306 $ys[3] += $h;
307 $yhwm++;
308 }
309 }
310 }
311 $t->{Bottom} = $yhwm;
312 $t->{Right} = $xhwm;
313 if (defined $xsb && $xs[2] > 0)
314 {
315 $xsb->MoveResizeWindow(@xs);
316 $cols -= $fcols;
317 if ($cols > 0)
318 {
319 $xsb->set($t->{Left}/$cols,$t->{Right}/$cols);
320 $xsb->MapWindow;
321 }
322 }
323 if (defined $ysb && $ys[3] > 0)
324 {
325 $ysb->MoveResizeWindow(@ys);
326 $rows -= $frows;
327 if ($rows > 0)
328 {
329 $ysb->set($t->{Top}/$rows,$t->{Bottom}/$rows);
330 $ysb->MapWindow;
331 }
332 }
333 }
334 }
335
336 sub QueueLayout
337 {
338 my ($m,$why) = @_;
339 $m->afterIdle(['Layout',$m]) unless ($m->{LayoutPending});
340 $m->{LayoutPending} |= $why;
341 }
342
343 sub SlaveGeometryRequest
344 {
345 my ($m,$s) = @_;
346 my ($row,$col) = @{$m->{Slave}{$s->PathName}};
347 my $sw = $s->ReqWidth;
348 my $sh = $s->ReqHeight;
349 my $sz = 0;
350 if ($sw > $m->{Width}[$col])
351 {
352 $m->{Width}[$col] = $sw;
353 $m->QueueLayout(_SlaveSize);
354 $sz++;
355 }
356 if ( (not defined ($m->{Height}[$row])) or $sh > $m->{Height}[$row])
357 {
358 $m->{Height}[$row] = $sh;
359 $m->QueueLayout(_SlaveSize);
360 $sz++;
361 }
362 if (!$sz)
363 {
364 $s->ResizeWindow($m->{Width}[$col],$m->{Height}[$row]);
365 }
366 }
367
368 sub get
369 {
370 my ($t,$row,$col) = @_;
371 return $t->{Row}[$row][$col];
372 }
373
374 sub LostSlave
375 {
376 my ($t,$s) = @_;
377 my $info = delete $t->{Slave}{$s->PathName};
378 if (defined $info)
379 {
380 my ($row,$col) = @$info;
381 $t->{Row}[$row][$col] = undef;
382 $s->UnmapWindow;
383 }
384 else
385 {
386 $t->BackTrace('Cannot find' . $s->PathName);
387 }
388 $t->QueueLayout(_SlaveChange);
389 }
390
391 sub clear {
392 my $self = shift;
393 my $rows = $self->cget(-rows);
394 my $cols = $self->cget(-columns);
395 foreach my $r (0 .. $rows-1) {
396 foreach my $c (0 .. $cols-1) {
397 my $old = $self->get( $r, $c );
398 next unless $old;
399 $self->LostSlave($old);
400 $old->destroy;
401 }
402 }
403 $self->_init;
404 $self->QueueLayout(_SlaveSize);
405 }
406
407 sub _init {
408 my $self = shift;
409 $self->{'Width'} = [];
410 $self->{'Height'} = [];
411 $self->{'Row'} = [];
412 $self->{'Slave'} = {};
413 $self->{'Top'} = 0;
414 $self->{'Left'} = 0;
415 $self->{'Bottom'} = 0;
416 $self->{'Right'} = 0;
417 $self->{LayoutPending} = 0;
418 }
419
420 sub put
421 {
422 my ($t,$row,$col,$w) = @_;
423 $w = $t->Label(-text => $w) unless (ref $w);
424 $t->ManageGeometry($w);
425 unless (defined $t->{Row}[$row])
426 {
427 $t->{Row}[$row] = [];
428 $t->{Height}[$row] = 0;
429 }
430 unless (defined $t->{Width}[$col])
431 {
432 $t->{Width}[$col] = 0;
433 }
434 my $old = $t->{Row}[$row][$col];
435 if (defined $old)
436 {
437 $old->UnmanageGeometry;
438 $t->LostSlave($old);
439 }
440 $t->{Row}[$row][$col] = $w;
441 $t->{Slave}{$w->PathName} = [$row,$col];
442 $t->SlaveGeometryRequest($w);
443 $t->QueueLayout(_SlaveChange);
444 return $old;
445 }
446
447 #
448 # configure methods
449 #
450
451 sub scrollbars
452 {
453 my ($t,$v) = @_;
454 if (@_ > 1)
455 {
456 $t->_configure(-scrollbars => $v);
457 $t->QueueLayout(_ScrollBars);
458 }
459 return $t->_cget('-scrollbars');
460 }
461
462 sub rows
463 {
464 my ($t,$r) = @_;
465 if (@_ > 1)
466 {
467 $t->_configure(-rows => $r);
468 if ($t->{Row} && @{$t->{Row}} > $r)
469 {
470 for my $y ($r .. $#{$t->{Row}})
471 {
472 for my $s (@{$t->{Row}[$y]})
473 {
474 $s->destroy if $s;
475 }
476 }
477 splice @{ $t->{Row} }, $r;
478 }
479 $t->QueueLayout(_RowColCount);
480 }
481 return $t->_cget('-rows');
482 }
483
484 sub fixedrows
485 {
486 my ($t,$r) = @_;
487 if (@_ > 1)
488 {
489 $t->_configure(-fixedrows => $r);
490 $t->QueueLayout(_RowColCount);
491 }
492 return $t->_cget('-fixedrows');
493 }
494
495 sub columns
496 {
497 my ($t,$r) = @_;
498 if (@_ > 1)
499 {
500 $t->_configure(-columns => $r);
501 if ($t->{Row})
502 {
503 for my $row (@{$t->{Row}})
504 {
505 for my $s (@$row[$r .. $#$row])
506 {
507 $s->destroy if $s;
508 }
509 { # FIXME? - Steve was getting warnings :
510 # splice() offset past end of array
511 local $^W = 0;
512 splice @$row, $r;
513 }
514 }
515 }
516 $t->QueueLayout(_RowColCount);
517 }
518 return $t->_cget('-columns');
519 }
520
521 sub fixedcolumns
522 {
523 my ($t,$r) = @_;
524 if (@_ > 1)
525 {
526 $t->_configure(-fixedcolumns => $r);
527 $t->QueueLayout(_RowColCount);
528 }
529 return $t->_cget('-fixedcolumns');
530 }
531
532 1;
533 __END__
534 sub Create
535 {
536 my $t = shift;
537 my $r = shift;
538 my $c = shift;
539 my $kind = shift;
540 $t->put($r,$c,$t->$kind(@_));
541 }
542
543 sub totalColumns
544 {
545 scalar @{shift->{'Width'}};
546 }
547
548 sub totalRows
549 {
550 scalar @{shift->{'Height'}};
551 }
552
553 sub Posn
554 {
555 my ($t,$s) = @_;
556 my $info = $t->{Slave}{$s->PathName};
557 return (wantarray) ? @$info : $info;
558 }
559
560 sub see
561 {
562 my $t = shift;
563 my ($row,$col) = (@_ == 2) ? @_ : @{$t->{Slave}{$_[0]->PathName}};
564 my $see = 1;
565 if (($row -= $t->cget('-fixedrows')) >= 0)
566 {
567 if ($row < $t->{Top})
568 {
569 $t->{Top} = $row;
570 $t->QueueLayout(_ViewChange);
571 $see = 0;
572 }
573 elsif ($row >= $t->{Bottom})
574 {
575 $t->{Top} += ($row - $t->{Bottom}+1);
576 $t->QueueLayout(_ViewChange);
577 $see = 0;
578 }
579 }
580 if (($col -= $t->cget('-fixedcolumns')) >= 0)
581 {
582 if ($col < $t->{Left})
583 {
584 $t->{Left} = $col;
585 $t->QueueLayout(_ViewChange);
586 $see = 0;
587 }
588 elsif ($col >= $t->{Right})
589 {
590 $t->{Left} += ($col - $t->{Right}+1);
591 $t->QueueLayout(_ViewChange);
592 $see = 0;
593 }
594 }
595 return $see;
596 }
597