File Coverage

blib/lib/LibWeb/Themes/Default.pm
Criterion Covered Total %
statement 108 158 68.3
branch 12 44 27.2
condition 44 146 30.1
subroutine 12 14 85.7
pod 0 9 0.0
total 176 371 47.4


\n"; "; \n" if ($gap_width); \n";
line stmt bran cond sub pod time code
1             #==============================================================================
2             # LibWeb::Themes::Default -- Default HTML widgets and theme for libweb
3             # applications
4              
5             package LibWeb::Themes::Default;
6              
7             # Copyright (C) 2000 Colin Kong
8             #
9             # This program is free software; you can redistribute it and/or
10             # modify it under the terms of the GNU General Public License
11             # as published by the Free Software Foundation; either version 2
12             # of the License, or (at your option) any later version.
13             #
14             # This program is distributed in the hope that it will be useful,
15             # but WITHOUT ANY WARRANTY; without even the implied warranty of
16             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17             # GNU General Public License for more details.
18             #
19             # You should have received a copy of the GNU General Public License
20             # along with this program; if not, write to the Free Software
21             # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22             #=============================================================================
23              
24             # $Id: Default.pm,v 1.6 2000/07/19 20:31:57 ckyc Exp $
25              
26             $VERSION = '0.02';
27              
28             #-#############################
29             # Use standard libraries.
30 2     2   1444 use strict;
  2         143  
  2         125  
31 2     2   14 use vars qw($VERSION @ISA);
  2         4  
  2         128  
32 2     2   20 use Carp;
  2         4  
  2         34117  
33              
34             #-#############################
35             # Use custom libraries.
36             require LibWeb::Core;
37              
38             #-#############################
39             # Inheritance.
40             @ISA = qw(LibWeb::Core);
41              
42             #-#############################
43             # Methods.
44             sub new {
45             #
46             # Params: $class [, $rc_file]
47             #
48             # - $class is the class/package name of this package, be it a string
49             # or a reference.
50             # - $rc_file is the absolute path to the rc file for LibWeb.
51             #
52             # Usage: my $object = new LibWeb::HTML::Themes([$rc_file]);
53             #
54 1     1 0 80 my ($class, $Class, $self);
55 1         4 $class = shift;
56 1   33     11 $Class = ref($class) || $class;
57            
58             # Inherit instance variables from the base class.
59 1         11 $self = $Class->SUPER::new(shift);
60 1         5 bless($self, $Class);
61             }
62              
63 0     0   0 sub DESTROY {}
64              
65             sub _parse_content {
66 5     5   10 my ($ref, @content_display);
67 5         88 my $content = $_[0];
68            
69 5         9 eval {
70 5         12 foreach (@$content) {
71 5         7 $ref = ref($_);
72 5 50       17 if ( $ref eq 'SCALAR' ) { push(@content_display, $$_); }
  0 50       0  
73 0         0 elsif ( $ref eq 'ARRAY' ) { push(@content_display, @$_); }
74 5         18 else { push(@content_display, $_); }
75             }
76             };
77 5 50       14 croak "$@" if ($@);
78 5         14 return \@content_display;
79             }
80              
81             sub table {
82             #
83             # Params: -content=>, [ -bg_color=>, -align=>, -valign=>, -width=> ]
84             #
85             # -content must be an ARRAY ref. to elements which are
86             # scalar/SCALAR ref./ARRAY ref. to plain HTML.
87             # -bg_color default is SITE_BG_COLOR.
88             # -align is the content's horizontal alignment; default is `left'.
89             # -valign is the content's vertical alignment; default is `top'.
90             # -width default is 100%.
91             #
92             # Return a SCALAR ref. to a formatted table in HTML format.
93             #
94 1     1 0 2 my ($self, $content, $bg_color, $align, $valign, $width,
95             $content_display);
96 1         2 $self = shift;
97 1         6 ($content, $bg_color, $align, $valign, $width)
98             = $self->rearrange( ['CONTENT', 'BG_COLOR', 'ALIGN', 'VALIGN', 'WIDTH'], @_ );
99            
100 1   50     7 $content ||= [' '];
101 1   33     9 $bg_color ||= $self->{SITE_BG_COLOR};
102 1   50     7 $align ||= 'left';
103 1   50     6 $valign ||= 'top';
104 1   50     5 $width ||= '100%';
105            
106 1         2 $content_display = _parse_content($content);
107            
108             #
109 1         12 return \<
110            
111            
112             @$content_display
113            
114            
115              
116             HTML
117             #
118             }
119              
120             sub titled_table {
121             #
122             # Params: -title=>, -content=>, [ -title_space=>, -title_align=>,
123             # -title_bg_color=>, -title_txt_color=>,
124             # -bg_color=>, align=>, valign=>,
125             # -cellpadding=>, -width=> ]
126             #
127             # -content must be an ARRAY ref. to elements which are
128             # scalar/SCALAR ref./ARRAY ref. to plain HTML.
129             # -title_space is the space ( ) prepended before (if -title_align is `left')
130             # or append after (if -title_align is `right') the title. It's always 0 if
131             # -title_align is `center'. Default is 2, i.e. `  '.
132             # -title_align default is center.
133             # -title_bg_color default is SITE_1ST_COLOR.
134             # -title_txt_color default is SITE_BG_COLOR.
135             # -bg_color default is if SITE_BG_COLOR.
136             # -align is the content's horizontal alignment; default is `left'.
137             # -valign is the content's vertical alignment; default is `top'.
138             # -cellpadding is the distance between the content and the `border' of table,
139             # default is 1.
140             # -width default is 100%.
141             #
142             # Return a SCALAR ref. to a formatted table in HTML format.
143             #
144 1     1 0 3 my ($self,
145             $title, $content, $title_space, $title_align, $title_bg_color,
146             $title_txt_color, $bg_color, $align, $valign, $cellpadding, $width,
147             $title_spacer, $content_display);
148 1         2 $self = shift;
149 1         7 ($title, $content, $title_space, $title_align, $title_bg_color,
150             $title_txt_color, $bg_color, $align, $valign, $cellpadding, $width)
151             = $self->rearrange( ['TITLE', 'CONTENT', 'TITLE_SPACE', 'TITLE_ALIGN',
152             'TITLE_BG_COLOR', 'TITLE_TXT_COLOR', 'BG_COLOR',
153             'ALIGN', 'VALIGN', 'CELLPADDING', 'WIDTH'],
154             @_ );
155            
156 1   50     6 $title ||= " ";
157 1   50     28 $content ||= [' '];
158 1   50     7 $title_align ||= 'center';
159 1 50       3 unless ( defined($title_space) ) {
160 1 50       4 $title_space = ( uc($title_align) eq 'CENTER' ) ? 0 : 2;
161             }
162 1         3 $title_spacer = ' ' x $title_space;
163 1 50       5 if ( uc($title_align) eq 'RIGHT' ) { $title = ''.$title.''.$title_spacer; }
  0         0  
164 1         3 else { $title = $title_spacer.''.$title.''; }
165 1   33     9 $title_bg_color ||= $self->{SITE_1ST_COLOR};
166 1   33     6 $title_txt_color ||= $self->{SITE_BG_COLOR};
167 1   33     5 $bg_color ||= $self->{SITE_BG_COLOR};
168 1   50     5 $align ||= 'left';
169 1   50     5 $valign ||= 'top';
170 1   50     4 $cellpadding ||= 1;
171 1   50     12 $width ||= '100%';
172            
173 1         333 $content_display = _parse_content($content);
174            
175             #
176 1         14 return \<
177            
178              
179            
180            
181            
182             $title
183            
184            
185              
186            
187            
188            
189             @$content_display
190            
191            
192              
193            
194              
195             HTML
196             #
197             }
198              
199             sub titled_table_enlighted {
200 1     1 0 4 shift->enlighted_titled_table(@_);
201             }
202              
203             sub enlighted_titled_table {
204             #
205             # Params: -title=>, -content=>, [ -title_space=>, -title_align=>,
206             # -title_bg_color=>,
207             # -title_txt_color=>, -title_border_color=>,
208             # -bg_color=>, -align=>, -valign=>,
209             # -cellpadding=>, -width=> ]
210             #
211             # -content must be an ARRAY ref. to elements which are
212             # scalar/SCALAR ref./ARRAY ref. to plain HTML.
213             # -title_space is the space ( ) prepended before (if -title_align is `left')
214             # or append after (if -title_align is `right') the title; default is 2,
215             # i.e. `  '. It's always 0 if -title_align if `center'.
216             # -title_align default is center.
217             # -title_bg_color default is SITE_LIQUID_COLOR3.
218             # -title_txt_color default is SITE_LIQUID_COLOR5.
219             # -title_border_color default is SITE_LIQUID_COLOR5.
220             # -bg_color default is if SITE_BG_COLOR.
221             # -align is the content's horizontal alignment; default is `left'.
222             # -valign is the content's vertical alignment; default is `top'.
223             # -cellpadding is the distance between the content and the `border' of table,
224             # default is 1.
225             # -width default is 100%.
226             #
227             # Return a SCALAR ref. to a formatted table in HTML format.
228             #
229 1     1 0 2 my ($self,
230             $title, $content, $title_space, $title_align, $title_bg_color,
231             $title_txt_color, $title_border_color, $bg_color, $align, $valign,
232             $cellpadding, $width,
233             $title_spacer, $content_display);
234 1         2 $self = shift;
235 1         15 ($title, $content, $title_space, $title_align, $title_bg_color,
236             $title_txt_color, $title_border_color, $bg_color, $align, $valign,
237             $cellpadding, $width)
238             = $self->rearrange( ['TITLE', 'CONTENT', 'TITLE_SPACE', 'TITLE_ALIGN',
239             'TITLE_BG_COLOR', 'TITLE_TXT_COLOR', 'TITLE_BORDER_COLOR',
240             'BG_COLOR', 'ALIGN', 'VALIGN', 'CELLPADDING', 'WIDTH'],
241             @_ );
242            
243 1   50     12 $title ||= " ";
244 1   50     6 $content ||= [' '];
245 1   50     5 $title_align ||= 'center';
246 1 50       5 unless ( defined($title_space) ) {
247 1 50       5 $title_space = ( uc($title_align) eq 'CENTER' ) ? 0 : 2;
248             }
249 1         3 $title_spacer = ' ' x $title_space;
250 1 50       4 if ( uc($title_align) eq 'RIGHT' ) { $title = "${title}${title_spacer}"; }
  0         0  
251 1         4 else { $title = "${title_spacer}${title}"; }
252 1   33     18 $title_bg_color ||= $self->{SITE_LIQUID_COLOR3};
253 1   33     6 $title_txt_color ||= $self->{SITE_LIQUID_COLOR5};
254 1   33     11 $title_border_color ||= $self->{SITE_LIQUID_COLOR5};
255 1   33     5 $bg_color ||= $self->{SITE_BG_COLOR};
256 1   50     12 $align ||= 'left';
257 1   50     6 $valign ||= 'top';
258 1   50     5 $cellpadding ||= 1;
259 1   50     11 $width ||= '100%';
260            
261 1         4 $content_display = _parse_content($content);
262            
263             #
264 1         21 return \<
265            
266              
267            
268            
269            
270            
271            
272             $title
273            
274            
275            
276            
277            
278              
279            
280            
281            
282             @$content_display
283            
284            
285            
286              
287            
288              
289             HTML
290             #
291             }
292              
293             sub bordered_table {
294             #
295             # Params: -content=>, [ -border_color=>, -bg_color=>, align=>, -valign=>,
296             # -cellpadding=>, -width=> ]
297             #
298             # -content must be an ARRAY ref. to elements which are
299             # scalar/SCALAR ref./ARRAY ref. to plain HTML.
300             # -border_color default is SITE_LIQUID_COLOR2.
301             # -bg_color default is if SITE_BG_COLOR.
302             # -align is the content's horizontal alignment; default is `left'.
303             # -valign is the content's vertical alignment; default is `top'.
304             # -cellpadding is the distance between the content and the `border' of table,
305             # default is 2.
306             # -width default is 100%.
307             #
308             # Return a SCALAR ref. to a formatted table in HTML format.
309             #
310 1     1 0 49 my ($self,
311             $content, $border_color, $bg_color, $align, $valign, $cellpadding, $width,
312             $content_display);
313 1         2 $self = shift;
314 1         13 ($content, $border_color, $bg_color, $align, $valign, $cellpadding, $width)
315             = $self->rearrange( ['CONTENT', 'BORDER_COLOR', 'BG_COLOR', 'ALIGN', 'VALIGN',
316             'CELLPADDING', 'WIDTH'], @_ );
317            
318 1   50     8 $content ||= [' '];
319 1   33     13 $border_color ||= $self->{SITE_LIQUID_COLOR2};
320 1   33     5 $bg_color ||= $self->{SITE_BG_COLOR};
321 1   50     6 $align ||= 'left';
322 1   50     5 $valign ||= 'top';
323 1   50     5 $cellpadding ||= 2;
324 1   50     9 $width ||= '100%';
325            
326 1         4 $content_display = _parse_content($content);
327            
328             #
329 1         17 return \<
330            
331              
332            
333            
334            
335             @$content_display
336            
337            
338              
339            
340              
341             HTML
342             #
343             }
344              
345             sub titled_bordered_table {
346 1     1 0 5 shift->bordered_titled_table(@_);
347             }
348              
349             sub bordered_titled_table {
350             #
351             # Params: -title=>, -content=>, [ -title_space=>, -title_align=>,
352             # -border_color=>,
353             # -title_txt_color=>,
354             # -bg_color=>, -align=>, -valign=>,
355             # -cellpadding=>, -width=> ]
356             #
357             # -content must be an ARRAY ref. to elements which are
358             # scalar/SCALAR ref./ARRAY ref. to plain HTML.
359             # -title_space is the space ( ) prepended before (if -title_align is `left')
360             # or append after (if -title_align is `right') the title; default is 2, i.e.
361             # `  '. If -title_align is `center', -title_space is always 0.
362             # -title_align default is center.
363             # -border_color default is SITE_1ST_COLOR.
364             # -title_txt_color default is SITE_BG_COLOR.
365             # -bg_color default is if SITE_BG_COLOR.
366             # -align is the content's horizontal alignment; default is `left'.
367             # -valign is the content's vertical alignment; default is `top'.
368             # -cellpadding is the distance between the content and the `border' of table,
369             # default is 1.
370             # -width default is 100%.
371             #
372             # Return a SCALAR ref. to a formatted table in HTML format.
373             #
374 1     1 0 2 my ($self,
375             $title, $content, $title_space, $title_align, $border_color,
376             $title_txt_color, $bg_color, $align, $valign, $cellpadding, $width,
377             $title_spacer, $content_display);
378 1         2 $self = shift;
379 1         8 ($title, $content, $title_space, $title_align, $border_color,
380             $title_txt_color, $bg_color, $align, $valign, $cellpadding, $width)
381             = $self->rearrange( ['TITLE', 'CONTENT', 'TITLE_SPACE', 'TITLE_ALIGN',
382             'BORDER_COLOR', 'TITLE_TXT_COLOR',
383             'BG_COLOR', 'ALIGN', 'VALIGN', 'CELLPADDING', 'WIDTH'],
384             @_ );
385            
386 1   50     7 $title ||= " ";
387 1   50     8 $content ||= [' '];
388 1   50     5 $title_align ||= 'center';
389 1 50       4 unless ( defined($title_space) ) {
390 1 50       4 $title_space = ( uc($title_align) eq 'CENTER' ) ? 0 : 2;
391             }
392 1         4 $title_spacer = ' ' x $title_space;
393 1 50       3 if ( uc($title_align) eq 'RIGHT' ) { $title = ''.$title.''.$title_spacer; }
  0         0  
394 1         3 else { $title = $title_spacer.''.$title.''; }
395 1   33     5 $border_color ||= $self->{SITE_1ST_COLOR};
396 1   33     5 $title_txt_color ||= $self->{SITE_BG_COLOR};
397 1   33     6 $bg_color ||= $self->{SITE_BG_COLOR};
398 1   50     5 $align ||= 'left';
399 1   50     5 $valign ||= 'top';
400 1   50     5 $cellpadding ||= 1;
401 1   50     4 $width ||= '100%';
402            
403 1         2 $content_display = _parse_content($content);
404            
405             #
406 1         15 return \<
407            
408              
409            
410            
411            
412             $title
413            
414            
415              
416            
417            
418            
419             @$content_display
420            
421            
422              
423            
424              
425             HTML
426             #
427             }
428              
429             sub tabber {
430             #
431             # Params:
432             #
433             # -tabs=>, -active=> [, -active_color=>, -fade_color=>,
434             # -tab_padding=>, -tab_width=>,
435             # -gap_width=>, -total_width=>,
436             # -base=>, -base_height=>, -left_stub=>,
437             # -right_stub=>, -left_stub_align=>,
438             # -right_stub_align=>, -left_stub_width=>,
439             # -right_stub_width=> ]
440             #
441             # Pre:
442             # - -tabs is an ARRAY reference to scalar/SCALAR reference
443             # - -active is a number indicating which tab is currently active (count from 0)
444             # - -active_color is the color for the active tab; default is SITE_1ST_COLOR
445             # - -fade_color is the color for non-active tabs; default is SITE_LIQUID_COLOR3
446             # - -tab_padding is the distance between a tab's content to its border, either
447             # in pixel or percentage; default is 0
448             # - -tab_width is the width of each tab, either in pixel or percentage; default
449             # is ( total_width / (2 * number of tabs) )
450             # - -gap_width is the distance between two adjacent tabs, either in pixel or
451             # percentage; default if 1%
452             # - -total_width is the width of the whole tabber, either in pixel or percentage;
453             # default is 100%
454             # - -base is a SCALAR reference to HTML to be put under the tabs
455             # - -base_height is the height of -base, either in pixel or percentage; default
456             # is 5. You do not want to specify this if you specify -base
457             # - base_align default is `left'
458             # - -left_stub is a SCALAR reference to HTML to be put to the left of the tabs
459             # - -right_stub is a SCALAR reference to HTML to be put to the right of the tabs
460             # - -left_stub_align is 'left', 'center' or 'right'; default is 'left'
461             # - -right_stub_align is 'left', 'center' or 'right'; default is 'right'
462             # - -left_stub_width is either in pixel or percentage, you may want to specify
463             # this if you specify -left_stub
464             # - -right_stub_width is either in pixel or percentage
465             #
466             # Post:
467             #
468             # Return a SCALAR ref. to a formatted tabbing navigation bar in HTML format.
469             #
470             my (
471 0     0 0   $self,
472             $tabs, $active, $active_color, $fade_color, $tab_padding, $tab_width,
473             $gap_width, $total_width, $base, $base_height, $base_align, $left_stub,
474             $right_stub, $left_stub_align, $right_stub_align, $left_stub_width,
475             $right_stub_width,
476             $num_tabs, $num_elements, $tabs_html, $count
477             );
478 0           $self = shift;
479             (
480 0           $tabs, $active, $active_color, $fade_color, $tab_padding, $tab_width,
481             $gap_width, $total_width, $base, $base_height, $base_align, $left_stub,
482             $right_stub, $left_stub_align, $right_stub_align, $left_stub_width,
483             $right_stub_width
484             ) = $self->rearrange(
485             [
486             'TABS','ACTIVE','ACTIVE_COLOR','FADE_COLOR','TAB_PADDING',
487             'TAB_WIDTH','GAP_WIDTH','TOTAL_WIDTH','BASE','BASE_HEIGHT',
488             'BASE_ALIGN','LEFT_STUB','RIGHT_STUB','LEFT_STUB_ALIGN',
489             'RIGHT_STUB_ALIGN','LEFT_STUB_WIDTH','RIGHT_STUB_WIDTH'
490             ], @_
491             );
492              
493 0   0       $active ||= 0;
494 0   0       $active_color ||= $self->{SITE_1ST_COLOR};
495 0   0       $fade_color ||= $self->{SITE_LIQUID_COLOR3};
496 0   0       $tab_padding ||= '0';
497 0 0         $gap_width = '1%' unless ( defined $gap_width );
498 0   0       $total_width ||= '100%';
499 0   0       $base_height ||= '5';
500 0   0       $base_align ||= 'left';
501 0   0       $base ||= \ ("
");
502 0           $tabs_html = '';
503            
504             # Determine how many columns the base should span.
505 0           eval { $num_tabs = scalar @{$tabs}; };
  0            
  0            
506 0 0         croak "$@" if ($@);
507 0 0 0       $num_elements = 2 * $num_tabs + ( ($left_stub || $left_stub_width) ? 1 : 0 ) + 1;
508              
509             # Determine the width for individual tab.
510 0 0         if ( $total_width =~ m:(.*)\%$: ) {
511 0   0       $tab_width ||= int ( $1 / (2*$num_tabs) ) . '%';
512             } else {
513 0   0       $tab_width ||= int ( $total_width / (2*$num_tabs) );
514             }
515            
516 0           eval {
517              
518             # Prepend the left stub.
519 0 0 0       if ($left_stub || $left_stub_width) {
520 0   0       $left_stub ||= \('');
521 0   0       $left_stub_align ||= 'left';
522 0 0 0       $left_stub_width ||= ( $tab_width =~ m:(.*)\%$: ) ?
523             int( (100 - $1 * $num_tabs)/2 ).'%' :
524             int( $total_width - $num_tabs * $tab_width);
525 0           $tabs_html .=
526             " $${left_stub} 
527             }
528            
529             # Create the tabs.
530 0           $count = 0;
531 0           foreach ( @{$tabs} ) {
  0            
532 0           my $the_tab;
533 0 0         my $color = ($count == $active) ? $active_color : $fade_color;
534 0 0         if ( ref $_ ) {
535 0           $the_tab = $$_;
536             } else {
537 0           $the_tab = $_;
538             }
539            
540 0           $tabs_html .=
541             "
".
542             " ${the_tab} 
543 0 0         $tabs_html .= " 
544              
545 0           $count++;
546             }
547              
548             # Append the right stub.
549 0   0       $right_stub ||= \('');
550 0   0       $right_stub_align ||= 'right';
551 0   0       $right_stub_width ||= ( 100 - $num_tabs ) . '%';
552 0           $tabs_html .= " $${right_stub} 
553             };
554 0 0         croak "$@" if ($@);
555              
556 0           return \<
557             $tabs_html
558            
559            
560            
$$base
561            
562            
563             HTML
564             }
565              
566             1;
567             __DATA__