TableLayout/TLCore.pm untested

filter: Statementstmt 0.0% 0 / 465 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 76 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 0.0% 0 / 90 Podpod 0.0% 0 / 75 totaltotal 0.0% 0 / 826 CC 40 SCAR 28.7
CC
40
Cov
0%
tl_setup
132.0
i_print
30.0
insert
30.0
CRAP 17.6
1 # ====================================================================
2 # Copyright (C) 1997,1998 Stephen Farrell <stephen@farrell.org>
3 #
4 # All rights reserved. This program is free software; you can
5 # redistribute it and/or modify it under the same terms as Perl
6 # itself.
7 #
8 # ====================================================================
9 # File: TLCore.pm
10 # Author: Stephen Farrell
11 # Created: August 1997
12 # Locations: http://www.palefire.org/~sfarrell/TableLayout/
13 # CVS $Id: TLCore.pm,v 1.27 1998/09/20 21:08:27 sfarrell Exp $
14 # ====================================================================
15  
16  
17 ## TODO:
18 ## +++. integrate with CGI.pm... i think this will make some ppl happy =)
19 ## 1. When add something like a hidden form component into a table,
20 ## make sure it doesn't add an (extraneous) new cell for it.
21 ## 2. Way to tell passcgi to NOT pass a certain key; similarly way to
22 ## blow-away a parameter. By the same token; wrt default
23 ## parameters, it should be possible to inheret parameters from a
24 ## given labeled class, and then add some of your own, blow some
25 ## away, and so on.
26 ## 5. obj2tag should also take a tag in case some people make objects
27 ## that are expensive to create
28 ## 7. make it so that Anchor and align collide in tl_getParameters()
29 ## so that you can't have both inadvertantly.
30 ## 10. BUG! If you insert a form into a containing table, you need to
31 ## do so before you insert another table into it, otherwise the
32 ## inner table doesn't know about the form. (maybe not a bug??,
33 ## but a design flaw?)
34 ## 11. cleaning up of circular references is only about 90% now--need
35 ## to get *all* of them (project for a rainy day =)
36 ## 12. should be able to insert default preferences for children at
37 ## any widget level. implement default parameters on top of
38 ## getAllChildren(). keep api backwards compatible, but allow
39 ## setting of def params for any component container.
40 ##
41 ## DONE
42 ## o fixed textarea
43 ## o any componentcontainer can contain a form.
44 ## o multi form components can take flag 'Default', which will do the
45 ## same as useData() on the form
46 ##
47  
48  
49  
50 package HTML::TableLayout::TLCore;
51 package HTML::TableLayout::TL_BASE;
52  
53 use HTML::TableLayout::Symbols;
54 use Carp;
55 use strict;
56  
57 ##
58 ## 1998/04/20 1:14pm default constructor--move specific processing
59 ## into tl_init()
60 ##
61 sub new {
62 my $this = {};
63 bless $this, shift;
64 $this->tl_init(@_);
65 return $this;
66 }
67  
68 sub tl_init { confess "cannot resurrect the dead" if shift->{WAS_DESTROYED} }
69  
70  
71 ##
72 ## tl_getParameters(): this is used internally to generate the
73 ## parameters list. It combines the parameters set as global with
74 ## those entered with the constructor for this object. Note that
75 ## tl_getParameters needs to be called "late" (like in tl_setup(), but
76 ## not in insert()).
77 ##
78 ## You'll find you need to override this when you have "fake"
79 ## parameters you're injecting (look at
80 ## HTML::TableLayout::Component::Text for an example)
81 ##
82 sub tl_getParameters {
83 my ($this) = @_;
84
85 confess "No WINDOW" unless $this->{TL_WINDOW};
86
87 my %gp = $this->{TL_WINDOW}->{PARAMETERS}->get($this);
88 if ($this->{TL_PARAMS}) {
89 my %h = (%gp, %{ $this->{TL_PARAMS} });
90 return %h;
91 }
92 else {
93 return %gp;
94 }
95 }
96  
97 ##
98 ## getParameter( param_name ): get a particular parameter from a
99 ## component. if there is a window, then it'll call
100 ## tl_getParameters() to get defaults, otherwise it returns just
101 ## whatever is in the TL_PARAMS hash
102 ##
103 sub getParameter {
104 my $this = shift;
105 my $param = shift;
106  
107 my %p =
108 ($this->{TL_WINDOW}) ? $this->tl_getParameters() : %{ $this->{TL_PARAMS} };
109 return (exists $p{$param}) ? $p{$param} : undef;
110 }
111  
112 ##
113 ## tl_getLocalParameters(): This gets you a reference, so you can
114 ## delete, insert, hang yourself, etc.
115 ##
116 sub tl_getLocalParameters { return shift->{TL_PARAMS} }
117  
118  
119 ##
120 ## tl_inheritParamsFrom(): This is here for widgets to be able to
121 ## inherit default properties from whatever they wish. For example,
122 ## if you make a widget that derives from Cell, you might want to make
123 ## it get the default parameters for cell as well. Just call
124 ## tl_inheritParamsFrom(cell()) "early" and that'll happen for you.
125 ##
126 ## Warning: this method might go away in a future release.
127 sub tl_inheritParamsFrom {
128 my ($this, $obj) = @_;
129 $this->{TL_PARAMS_ISA} = $obj;
130 }
131  
132 ##
133 ## setParams() takes a hash and sets the params hash
134 ## accordingly... this is used when you want to change such AFTER the
135 ## constructor is called (the normal place for doing so).
136 ##
137  
138 sub setParams {
139 my ($this, %params) = @_;
140 foreach ( keys %params ) {
141 $this->{TL_PARAMS}->{$_} = $params{$_};
142 }
143 return $this;
144 }
145  
146 ##
147 ## need to add explicit memory management here
148 ##
149 sub tl_destroy {
150 my ($this) = @_;
151 undef $this->{TL_PARAMS};
152 undef $this->{TL_PARAMS_ISA};
153 $this->{WAS_DESTROYED} = 1;
154 ## warn "DESTROY $this\n";
155 }
156  
157 ##
158 ## whip up a copy of an object--note this is a first attempt...
159 ##
160 sub clone {
161 my ($this) = @_;
162 ##
163 ## do this??
164 ##
165 delete $this->{TL_WINDOW};
166 delete $this->{TL_CONTAINER};
167 delete $this->{TL_FORM};
168  
169 use Data::Dumper;
170 my $d = Data::Dumper->new([$this],[qw(enolc)]);
171 $d->Purity(1);
172 $d->Deepcopy(1);
173 my $enolc;
174 eval $d->Dump();
175 return $enolc;
176 }
177  
178 ##
179 ## accessors -- these only work in tl_setup(), not tl_init()!
180 ##
181 sub getForm { return shift->{TL_FORM} }
182 sub getContainer { return shift->{TL_CONTAINER} }
183 sub getWindow { return shift->{TL_WINDOW} }
184  
185 ## sub DESTROY { warn "DESTROYING " . shift . "\n" }
186  
187 ## --------------------------------------------------------------------
188 ##
189 ## Parameters package--this handles the default parameters
190 ##
191 package HTML::TableLayout::Parameters;
192 use HTML::TableLayout::Symbols;
193 @HTML::TableLayout::Parameters::ISA=qw(HTML::TableLayout::TL_BASE);
194  
195 sub tl_init {
196 my $this = shift;
197 $this->{DATA} = shift;
198 $this->SUPER::tl_init(@_);
199 }
200
201  
202 ##
203 ## FAST & destructive
204 ##
205 sub set {
206 my ($this, $obj, %hash) = @_;
207 $this->{DATA}->{$this->_obj2tag($obj)} = \%hash;
208 }
209  
210 ##
211 ## SLOW & non-destructive
212 ##
213 sub insert {
214 my ($this, $obj, %hash) = @_;
215 foreach (keys %hash) {
216 $this->{DATA}->{$this->_obj2tag($obj)}->{$_} = $hash{$_};
217 }
218 }
219  
220 sub delete {
221 my ($this, @keys) = @_;
222 map { delete $this->{DATA}->{$_} } @keys;
223 }
224  
225 sub get {
226 my ($this, $obj) = @_;
227 my $h = $this->{DATA}->{$this->_obj2tag($obj)};
228
229 if ($h) {
230 return %$h;
231 }
232 else {
233 return ();
234 }
235 }
236  
237 ##
238 ## This is here b/c when you create new widgets yourself, they can
239 ## take advantage of the default parameters system without having to
240 ## add any code yourself to the widget to handle this specifically.
241 ## In order to do this, the way it relates defaults with classes is by
242 ## being given an instance of the class, and then it "recognizes" that
243 ## class in the future.
244 ##
245 sub _obj2tag {
246 my ($this, $obj) = @_;
247
248 ##
249 ## when you make a widget, you might want to be able to inheret the
250 ## params as if you were the same kind of hting. You can call
251 ## $this->tl_inheritParamsFrom(constructor) and you're set.
252 ##
253 my $name = $obj->{TL_PARAMS_ISA} || $obj;
254
255 my $tag;
256 ##
257 ## _obj2tag() is called a lot, so this regexp is a bit expensive...
258 ##
259 ($tag = $name) =~ s/^([\w:]+)=.*/$1/;
260 return $tag;
261 }
262  
263  
264  
265 # ---------------------------------------------------------------------
266  
267 package HTML::TableLayout::_Anchor;
268 use HTML::TableLayout::Symbols;
269 @HTML::TableLayout::_Anchor::ISA=qw(HTML::TableLayout::TL_BASE);
270  
271 sub tl_init {
272 my $this = shift;
273 $this->{anchor} = shift;
274 $this->SUPER::tl_init(@_);
275 }
276  
277 sub value {
278 my ($this) = @_;
279 SWITCH: {
280 my $case = $this->{anchor} || return ();
281 ($case eq "top") and return (valign=>"top");
282 ($case eq "right") and return (align=>"right");
283 ($case eq "bottom") and return (valign=>"bottom");
284 ($case eq "left") and return (align=>"left");
285 ($case eq "center") and return (align=>"center" ,valign=>"middle");
286 ($case eq "ne") and return (align=>"right" ,valign=>"top");
287 ($case eq "se") and return (align=>"right" ,valign=>"bottom");
288 ($case eq "sw") and return (align=>"left" ,valign=>"bottom");
289 ($case eq "nw") and return (align=>"left" ,valign=>"top");
290 ($case =~ /^n/) and return (align=>"center" ,valign=>"top");
291 ($case =~ /^e/) and return (align=>"right" ,valign=>"center");
292 ($case =~ /^s/) and return (align=>"center" ,valign=>"bottom");
293 ($case =~ /^w/) and return (align=>"left" ,valign=>"center");
294 die("unknown anchor \"$case\"");
295 }
296 }
297  
298 # ---------------------------------------------------------------------
299  
300  
301 package HTML::TableLayout::Window;
302 use HTML::TableLayout::Symbols;
303 @HTML::TableLayout::Window::ISA=qw(HTML::TableLayout::ComponentContainer);
304  
305  
306  
307 sub tl_init {
308 my $this = shift;
309 my $def_params = shift;
310 my $title = shift;
311 my %params = @_;
312 $this->SUPER::tl_init(@_);
313  
314 ## FIXME -- what does this do?
315 defined $params{Cacheable} and $this->{CACHEABLE} = 1;
316 delete $params{Cacheable};
317  
318 if ($def_params) {
319 $this->{PARAMETERS} = $def_params;
320 }
321 else {
322 ##
323 ## you can pass in null parameters and everything will behave
324 ## properly, but just defaults
325 ##
326 $this->{PARAMETERS} = HTML::TableLayout::Parameters->new();
327 }
328 $this->{title} = $title;
329  
330 $this->{headers} = [];
331 $this->{tables} = [];
332 $this->{scripts} = [];
333 $this->{TL_WINDOW} = $this; # these get cleaned up by SUPER::tl_destroy
334 $this->{TL_CONTAINER} = $this;
335 $this->{INDENT} = 0;
336 $this->{CACHEABLE} = 0;
337 }
338  
339 sub tl_destroy {
340 my $this = shift;
341 $this->{PARAMETERS}->tl_destroy();
342 undef $this->{PARAMETERS};
343 foreach (@{ $this->{headers} }) { $_->tl_destroy() }
344 foreach (@{ $this->{tables} }) { $_->tl_destroy() }
345 foreach (@{ $this->{scripts} }) { $_->tl_destroy() }
346 $this->SUPER::tl_destroy();
347 }
348  
349 sub insert {
350 my ($this, $component) = @_;
351
352 ##
353 ## Sanity checking on input (this is a bit broken, of course--is
354 ## there an equivalent for "ref" that checks for object's classes?
355 ##
356 ref $component or die("$component must be an object");
357 $component->isa("HTML::TableLayout::Component") or
358 die("$component must be a component");
359
360
361 ##
362 ## Pseudo (runtime) overloading...
363 ##
364 if ($component->isa("HTML::TableLayout::Script")) {
365 push @{ $this->{scripts} }, $component;
366 }
367 elsif ($component->isa("HTML::TableLayout::WindowHeader")) {
368 push @{ $this->{headers} }, $component;
369 }
370 elsif ($component->isa("HTML::TableLayout::Table")) {
371 push @{ $this->{tables} }, $component;
372 }
373 else {
374 die("cannot insert a $component into a window");
375 }
376
377 $this->SUPER::insert($component);
378 }
379  
380 sub render {
381 my $this = shift;
382 $this->print();
383 $this->tl_destroy();
384 }
385  
386 sub print {
387 my ($this) = @_;
388
389 ##
390 ## Fire off the first traversal of the heirarchy where "setup" gets
391 ## done. This is where componentcontainers tell their components
392 ## their context, and where things that need to know about or affect
393 ## other things around them can do so.
394 ##
395 $this->tl_setup();
396
397
398 print "Content-type: text/html\n\n";
399  
400 $this->i_print("<HTML");
401 $this->_indentIncrement();
402 $this->i_print("><HEAD><TITLE>$this->{title}</TITLE></HEAD");
403
404 foreach (@{ $this->{scripts} }) { $_->tl_print() }
405
406 $this->i_print("><BODY".params($this->tl_getParameters())."");
407 $this->_indentIncrement();
408
409 foreach (@{ $this->{headers} }) { $_->tl_print() }
410
411 foreach (@{ $this->{tables} }) { $_->tl_print() }
412 $this->_indentDecrement();
413 $this->i_print("></BODY");
414 $this->_indentDecrement();
415 $this->i_print("></HTML>");
416 $this->i_print();
417 }
418  
419 sub toString {
420 my ($this) = @_;
421 $this->{CACHEABLE} = 1;
422 $this->print();
423 return $this->{TEXT_CACHE};
424 }
425  
426 ##
427 ## i_print() and f_print() are what components/widgets use to print
428 ## themselves out. When you use these, you're guaranteed to get at
429 ## least two things for free: (1) proper indenting of code (as long as
430 ## you do NOT use any newlines) (2) correct behavior depending on
431 ## whether we call print() or toString() on the window. I.E., you do
432 ## not *need* to use these in your widgets, as long as you accept the
433 ## aforementioned losses (and perhaps others in the future...
434 ##
435  
436 ##
437 ## i_print(): indent print (both i_print() and f_print() take care of
438 ## whether to pack the result into a string or output it right away
439 ## with print())
440 ##
441 sub i_print {
442 my ($this,$text) = @_;
443 my $cacheable = $this->{CACHEABLE};
444 my $i;
445 my $indent = $this->{INDENT};
446
447
448 ##
449 ## This is the only place where newlines come from!
450 ##
451 $cacheable ? $this->{TEXT_CACHE} .= "\n" : print "\n";
452
453 ##
454 ## print the indent spaces so it looks pretty
455 ##
456 if ($cacheable) {
457 $this->{TEXT_CACHE} .= " " x $indent;
458 }
459 else {
460 print " " x $indent;
461 }
462
463 ##
464 ## print the content
465 ##
466 if ($cacheable) {
467 if ($text) {
468 $this->{TEXT_CACHE} ||= "";
469 $this->{TEXT_CACHE} .= $text;
470 }
471 }
472 else {
473 print $text;
474 }
475 }
476  
477 ##
478 ## f_print(): flush print
479 ##
480 sub f_print {
481 my ($this,$text) = @_;
482 if ($this->{CACHEABLE}) {
483 $this->{TEXT_CACHE} .= $text;
484 }
485 else {
486 print $text;
487 }
488 }
489  
490  
491 sub _indentIncrement { shift->{INDENT}++ }
492 sub _indentDecrement { shift->{INDENT}-- }
493 sub _getIndent { return shift->{INDENT} }
494 sub _incrementNumForms { shift->{NUM_FORMS}++ }
495 sub _getNumForms { return shift->{NUM_FORMS} || 0 }
496  
497  
498  
499 # ---------------------------------------------------------------------
500  
501 package HTML::TableLayout::Table;
502 use HTML::TableLayout::Symbols;
503 use Carp;
504 @HTML::TableLayout::Table::ISA=qw(HTML::TableLayout::ComponentContainer);
505  
506 sub tl_init {
507 my $this = shift;
508 $this->SUPER::tl_init(@_);
509 $this->{rowindex} = 0;
510 }
511  
512 sub tl_destroy {
513 my $this = shift;
514 undef $this->{rowindex};
515 foreach (0.. $#{ $this->{rows} }) {
516 $this->{rows}->[$_]->tl_destroy();
517 undef $this->{rows}->[$_];
518 }
519 undef $this->{rows};
520 $this->SUPER::tl_destroy();
521 }
522
523  
524 sub insert {
525 my ($this, $c, $br) = @_;
526 if (! ref $c) {
527 my $temp = $c;
528 $c = HTML::TableLayout::Cell->new();
529 $c->insert($temp,$br);
530 }
531 $this->SUPER::insert($c, $br);
532 }
533  
534  
535 sub tl_setup {
536 my ($this) = @_;
537
538 $this->tl_setup_form();
539
540 my $first_row = 1;
541 my $i;
542 foreach $i (0..$#{ $this->{TL_COMPONENTS} }) {
543
544
545 ##
546 ## We can use $c for the most part, unless we are instrumenting a
547 ## change in what one of the components actually is.
548 ##
549 my $c = $this->{TL_COMPONENTS}->[$i];
550
551 my $cell;
552 if ($c->isa("HTML::TableLayout::Cell")) {
553
554 ##
555 ## if the cell has a header, then we slam it into an embedded
556 ## table. this procedure is too complex, and this code should
557 ## probably be encapsulated elsewhere, or made simpler.
558 ##
559
560 my $h;
561 if ($h = $c->getHeader()) {
562 $c->_forgetIHaveAHeader();
563
564 ##
565 ## lie to the header about its context so we can ask it for
566 ## it's orientation here. But we mean well... so it's ok.
567 ## Plus it'll get the correct context in a bit.
568 ##
569 $h->tl_setContext($this);
570
571 my ($t, $o);
572 $o = $h->orientation();
573
574
575 ##
576 ## Replace, in the components array, the original cell with a
577 ## new one that contains it (and its header) in a table.
578 ##
579 $this->{TL_COMPONENTS}->[$i] = $cell
580 = HTML::TableLayout::Cell->new(Anchor=>$o);
581
582
583 ##
584 ## $cell needs to inherit params from $c, $c needs to be
585 ## cleaned of most (all?) parameters.
586 ##
587 ## FIXME--there is no way this is fully correct...
588 ##
589 if (exists $c->{TL_PARAMS}->{colspan}) {
590 $cell->{TL_PARAMS}->{colspan} = $c->{TL_PARAMS}->{colspan};
591 delete $c->{TL_PARAMS}->{colspan};
592 }
593
594 if (exists $c->{TL_PARAMS}->{rowspan}) {
595 $cell->{TL_PARAMS}->{rowspan} = $c->{TL_PARAMS}->{rowspan};
596 delete $c->{TL_PARAMS}->{rowspan};
597 }
598
599 $cell->insert($t=HTML::TableLayout::Table
600 ->new(width=>"100%",
601 height=>"100%",
602 columns=>($o eq "top" or
603 $o eq "bottom") ? 1 : 2));
604 if ($o eq "top" or $o eq "left") {
605 $t->insert($h); # $h gets correct context here...
606 $t->insert($c);
607 }
608 else {
609 $t->insert($c);
610 $t->insert($h); # ...or here.
611 }
612 }
613 else {
614 $this->{TL_COMPONENTS}->[$i] = $cell = $c;
615 }
616 }
617 else {
618 $this->{TL_COMPONENTS}->[$i] = $cell =
619 HTML::TableLayout::Cell->new()->insert($c);
620 }
621
622
623 ##
624 ## ...now we have a cell for sure.
625 ##
626 $cell->tl_setContext($this);
627
628 ## =================================================================
629 ## o Pack resulting Cells into Table
630 ## =================================================================
631
632 ##
633 ## Take care of adding it to the row
634 ##
635 my $row;
636 if ($first_row) {
637 $first_row = 0;
638 ##
639 ## Bootstrap the first row
640 ##
641 $row = $this->{rows}->[0]
642 = HTML::TableLayout::_Row->new($this,
643 $this->{TL_WINDOW},
644 $this->{TL_FORM});
645 $row->insert($cell) or confess "insert failed; colspan > columns ??";
646 }
647 elsif (($row = $this->{rows}->[$this->{rowindex}])->insert($cell)) {
648 ##
649 ## Ok--successfully added cell to current row
650 ##
651 }
652 else {
653 ##
654 ## Hmm... need to create a new row (ok)
655 ##
656 $this->_removeOldSpanningCells();
657
658 my $n_ridx = ++$this->{rowindex};
659 $row = $this->{rows}->[$n_ridx]
660 = HTML::TableLayout::_Row->new($this,
661 $this->{TL_WINDOW},
662 $this->{TL_FORM});
663
664 if (! $row->insert($cell)) {
665 ##
666 ## This is error condition (!)
667 ##
668 my $cs = $cell->getColspan();
669 my $cols = $this->getColumns();
670 if ($cs > $cols) {
671 confess "colspan [$cs] exceeds max number of columns [$cols]";
672 }
673 else {
674 confess "?? cannot pack; colspan [$cs] and cols [$cols]";
675 }
676 }
677 }
678 $cell->tl_setup();
679 }
680  
681 }
682  
683 sub tl_print {
684 my ($this) = @_;
685 my $w = $this->{TL_WINDOW};
686 my $p = params($this->tl_getParameters()) || "";
687 $w->i_print("><TABLE $p");
688 if ($this->{form_is_mine}) { $this->{TL_FORM}->tl_print() }
689 $w->_indentIncrement();
690 foreach(@{ $this->{rows} }) { $_->tl_print() };
691 $w->_indentDecrement();
692 if ($this->{form_is_mine}) { $this->{TL_FORM}->_print_end() }
693 $w->i_print("></TABLE");
694 }
695  
696  
697 sub getColumns { return shift->{TL_PARAMS}->{columns} || 1 }
698 sub setColumns { shift->{TL_PARAMS}->{columns} = pop }
699  
700 ##
701 ## A "spanning cell" is a cell having a colspan > 1 and as such is
702 ## pertinent while packing subsequent rows. It needs to be handled
703 ## explicitely since it affects how many cells will fit into later
704 ## rows. These functions are used to keep track of cells in this
705 ## state, and do appropriate calculations and removing of old or stale
706 ## "spanning cells".
707 ##
708  
709 sub _insertSpanningCell {
710 my ($this, $cell) = @_;
711 push @{ $this->{spanning_cells} },
712 HTML::TableLayout::_SpanningCell->new($cell,
713 $this->{TL_WINDOW},
714 $this->{TL_FORM})
715 }
716  
717 sub _colspanOfSpanningCells {
718 my ($this) = @_;
719 my $sum = 0;
720 my $sc;
721 foreach $sc (@{ $this->{spanning_cells} }) {
722 $sum += $sc->getColspan();
723 }
724 return $sum;
725 }
726  
727 sub _removeOldSpanningCells {
728 my ($this) = @_;
729 my @old = @{ $this->{spanning_cells} };
730 my (@new, $sc);
731 foreach $sc (@old) {
732 if ($sc->decrement() > 0) {
733 push @new, $sc;
734 }
735 }
736 $this->{spanning_cells} = \ @new;
737 }
738  
739  
740  
741 # ---------------------------------------------------------------------
742 package HTML::TableLayout::_Row;
743 use HTML::TableLayout::Symbols;
744 @HTML::TableLayout::_Row::ISA=qw(HTML::TableLayout::ComponentContainer);
745  
746 ##
747 ## Some things about rows:
748 ##
749 ## o Rows happen automagically, hence the _ (aka private)
750 ##
751 ## o note that there are no parameters for the row--everyting that can
752 ## be set in the row can also be set in the cell (<TD>), so that's
753 ## where it'll be set.
754 ##
755 ## o We don't have to worry about the timing of rows b/c they are not
756 ## created until the table is "setup", which is "late". Hence we
757 ## can set up the context directly in the constructor.
758 ##
759  
760  
761 sub tl_init {
762 my $this = shift;
763 $this->{TL_CONTAINER} = shift;
764 $this->{TL_WINDOW} = shift;
765 $this->{TL_FORM} = shift;
766 $this->{columns} = $this->{TL_CONTAINER}->getColumns();
767 $this->SUPER::tl_init(@_);
768 }
769  
770 sub tl_destroy {
771 my $this = shift;
772 undef $this->{columns};
773 $this->SUPER::tl_destroy();
774 }
775  
776  
777 sub setRSOffset { shift->{rs_offset} = pop }
778 sub getRSOffset { return shift->{rs_offset} || 0 }
779  
780  
781 sub getVacantSlots {
782 my ($this) = @_;
783 my $spaces = $this->{columns};
784 my $cs_effect = sum(map { $_->getColspan() } @{ $this->{TL_COMPONENTS} });
785 my $rs_effect = $this->{TL_CONTAINER}->_colspanOfSpanningCells();
786 my $rs_offset = $this->getRSOffset();
787 my $filled = $cs_effect + $rs_effect - $rs_offset;
788 return $spaces - $filled;
789 }
790  
791 sub isFull { return shift->getVacantSlots() <= 0 }
792  
793  
794 ##
795 ## this insert() returns 1 if it is able to insert the cell, and 0 if
796 ## it is not. In most cases, returning 0 is normal--it's just time to
797 ## create a new row.
798 ##
799 sub insert {
800 my ($this, $cell) = @_;
801
802
803 return 0 if ($this->getVacantSlots() - $cell->getColspan() < 0);
804
805
806 if ($cell->tl_getContainer() ne $this) {
807 $cell->tl_setContext($this);
808 }
809
810 if ($cell->getRowspan() > 1) {
811 $this->{TL_CONTAINER}->_insertSpanningCell($cell);
812 $this->setRSOffset($this->getRSOffset()+$cell->getColspan());
813 }
814
815 $this->SUPER::insert($cell);
816 }
817  
818  
819  
820  
821 sub tl_print {
822 my ($this) = @_;
823 ##
824 ## The following check is disabled, but it *might* be re-enabled in
825 ## the future. The impact would be that the table must be CORRECTLY
826 ## filled or else this will layoutmanager will die(). Perhaps there
827 ## will be a flag to set for this behavior.
828 ##
829
830 #if (! $this->isFull() ) { die("row is not full") }
831
832 my $w = $this->{TL_WINDOW};
833
834 $w->i_print("><TR");
835
836 $w->_indentIncrement();
837 foreach(@{ $this->{TL_COMPONENTS} }) { $_->tl_print() };
838 $w->_indentDecrement();
839 $w->i_print("></TR");
840 }
841 # ---------------------------------------------------------------------
842  
843 package HTML::TableLayout::Cell;
844 use HTML::TableLayout::Symbols;
845 @HTML::TableLayout::Cell::ISA=qw(HTML::TableLayout::ComponentContainer);
846  
847  
848 sub tl_destroy {
849 my $this = shift;
850 undef $this->{cell_header};
851 $this->SUPER::tl_destroy();
852 }
853  
854  
855 sub tl_getParameters {
856 my ($this) = @_;
857 my %r = ($this->{TL_WINDOW}->{PARAMETERS}->get($this),
858 %{ $this->{TL_PARAMS} },
859 colspan=>$this->getColspan(),
860 rowspan=>$this->getRowspan(),
861 width=>$this->getWidth());
862
863 return %r;
864 }
865  
866 sub insert {
867 my ($this, $c, $br) = @_;
868 ##
869 ## if $c is a header, need to treat specially
870 ##
871 if (ref $c) {
872 if ($c->isa("HTML::TableLayout::CellHeader")) {
873 if ($this->{"cell_header"}) {
874 die("There can only be one header per cell | $this");
875 }
876 $this->{"cell_header"} = $c;
877 }
878 elsif ($c->isa("HTML::TableLayout::Cell")) {
879 die("Cannot insert a cell into a cell!");
880 }
881 else {
882 $this->SUPER::insert($c, $br);
883 }
884 }
885 else {
886 $this->SUPER::insert($c, $br);
887 }
888 return $this;
889 }
890  
891  
892 sub tl_print {
893 my ($this) = @_;
894
895
896 my $w = $this->{TL_WINDOW};
897
898 ##
899 ## if "form_is_mine" flag is set, then we know we contain a form,
900 ## and that this form should be printed AND IT HAS NOT ALREADY BEEN
901 ## PRINTED BY A CONTAINING OBJECT.
902 ##
903
904
905 $this->{form_is_mine} and $this->{TL_FORM}->tl_print();
906 $w->i_print("><TD".params($this->tl_getParameters())."");
907 $w->_indentIncrement();
908
909
910 foreach (0..$#{ $this->{TL_COMPONENTS} }) {
911 $this->{TL_COMPONENTS}->[$_]->tl_print();
912 $this->{TL_BREAKS}->[$_] and $w->i_print("><BR");
913 }
914
915 $w->_indentDecrement();
916 $w->i_print("></TD");
917 $this->{form_is_mine} and $this->{TL_FORM}->_print_end();
918
919 }
920  
921 sub getWidth {
922 my ($this) = @_;
923 return $this->{TL_PARAMS}->{width} ||
924 int(100 * ($this->getColspan() /
925 $this->{TL_CONTAINER}->{TL_CONTAINER}->getColumns())) . "%";
926 }
927  
928  
929 sub getColspan { return shift->{TL_PARAMS}->{colspan} || 1 }
930 sub getRowspan { return shift->{TL_PARAMS}->{rowspan} || 1 }
931  
932  
933 sub getForm { return shift->{TL_FORM} }
934 sub getHeader { return shift->{"cell_header"} }
935  
936 sub setColspan { shift->{TL_PARAMS}->{colspan} = pop }
937 sub setRowspan { shift->{TL_PARAMS}->{rowspan} = pop }
938  
939  
940 sub _forgetIHaveAHeader { delete shift->{"cell_header"} }
941  
942  
943 # ---------------------------------------------------------------------
944  
945 package HTML::TableLayout::_SpanningCell;
946 use HTML::TableLayout::Symbols;
947 @HTML::TableLayout::_SpanningCell::ISA=qw(HTML::TableLayout::TL_BASE);
948  
949  
950 ##
951 ## Like rows, these happen "late" and so we can put the context info
952 ## right in the constructor
953 ##
954  
955 sub tl_init {
956 my $this = shift;
957 $this->{TL_CONTAINER} = shift;
958 $this->{size} = $this->{TL_CONTAINER}->getRowspan();
959 $this->{TL_WINDOW} = shift;
960 $this->{TL_FORM} = shift;
961 $this->SUPER::tl_init(@_);
962 }
963  
964 sub decrement { return --shift->{size} }
965 sub increment { return ++shift->{size} }
966 sub getColspan { return shift->{TL_CONTAINER}->getColspan() }
967  
968  
969 # ---------------------------------------------------------------------
970  
971 package HTML::TableLayout::CellHeader;
972 use HTML::TableLayout::Symbols;
973 @HTML::TableLayout::CellHeader::ISA
974 =qw(HTML::TableLayout::ComponentContainer HTML::TableLayout::Cell);
975  
976 ##
977 ## The inheretence is kind of whack here. Basically, this thing sits
978 ## in a table and hence should be a cell. HOWEVER, we don't want to
979 ## use cell's tl_getParameters(), but rather the one from
980 ## Component... funny thing is that cell is a component, so by using
981 ## multiple inheretence here, we're a cell and twice a component. but
982 ## it seems to work so what the fuck^H^H^H^Hfooey.
983 ##
984  
985  
986 sub tl_init {
987 my $this = shift;
988 my $h = shift;
989 $this->SUPER::tl_init(@_);
990
991 if (ref $h) {
992 $this->{TL_COMPONENTS}->[0] = $h;
993 }
994 else {
995 $this->{TL_COMPONENTS}->[0]
996 = HTML::TableLayout::Component::Text->new($h);
997 }
998 }
999  
1000  
1001  
1002 sub tl_print {
1003 my ($this) = @_;
1004
1005 my $w = $this->{TL_WINDOW};
1006 my %params = $this->tl_getParameters();
1007 delete $params{Orientation};
1008
1009 $w->i_print("><TH".params(%params)."");
1010 $w->_indentIncrement();
1011 $this->{TL_COMPONENTS}->[0]->tl_print();
1012 $w->_indentDecrement();
1013 $w->i_print("></TH");
1014 }
1015  
1016  
1017 ##
1018 ## Note that "Orientation" is capitalized because it is a pseudo
1019 ## parameter and does not make it through to the HTML.
1020 ##
1021 sub orientation {
1022 my ($this) = @_;
1023 my %p = $this->tl_getParameters();
1024 return $p{Orientation} || "top";
1025 }
1026  
1027  
1028  
1029 # ---------------------------------------------------------------------
1030  
1031 package HTML::TableLayout::WindowHeader;
1032 use HTML::TableLayout::Symbols;
1033 @HTML::TableLayout::WindowHeader::ISA=
1034 qw(HTML::TableLayout::ComponentContainer);
1035  
1036  
1037 sub tl_init {
1038 my $this = shift;
1039 $this->{H} = shift;
1040 my $xx = shift;
1041 $this->SUPER::tl_init(@_);
1042 $this->{TL_COMPONENTS}->[0] = $xx;
1043 }
1044  
1045  
1046 sub tl_setup {
1047 my ($this) = @_;
1048 if (! ref $this->{TL_COMPONENTS}->[0]) {
1049 $this->{TL_COMPONENTS}->[0] =
1050 HTML::TableLayout::Component::Text->new($this->{TL_COMPONENTS}->[0]);
1051 }
1052
1053 $this->SUPER::tl_setup();
1054 }
1055  
1056 sub tl_print {
1057 my ($this) = @_;
1058
1059 my $w = $this->{TL_WINDOW};
1060 if ($this->{H}) {
1061 $w->i_print("><H".$this->{H}.params($this->tl_getParameters())."");
1062 $w->_indentIncrement();
1063 $this->{TL_COMPONENTS}->[0]->tl_print();
1064 $w->_indentDecrement();
1065 $w->f_print("></H$this->{H}");
1066 }
1067 else {
1068 $w->i_print();
1069 $this->{TL_COMPONENTS}->[0]->tl_print();
1070 }
1071 }
1072  
1073 # ---------------------------------------------------------------------
1074 package HTML::TableLayout::Script;
1075 use HTML::TableLayout::Symbols;
1076 @HTML::TableLayout::Script::ISA=qw(HTML::TableLayout::Component);
1077  
1078  
1079 sub tl_init {
1080 my $this = shift;
1081 $this->{script} = shift;
1082 $this->SUPER::tl_init(@_);
1083 }
1084  
1085 sub tl_print {
1086 my ($this) = @_;
1087 my $w = $this->{TL_WINDOW};
1088 my $p = params($this->tl_getParameters()) || "";
1089 $w->i_print("><SCRIPT $p>\n<!--\n");
1090 $w->f_print($this->{"script"});
1091 $w->f_print("\n//-->");
1092 $w->i_print("</SCRIPT");
1093 }
1094  
1095 1;