# Copyright (c) 1999-2009 bivio Software, Inc. All rights reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: Image.pm,v 2.14 2010/11/12 00:11:47 moeller Exp $ package Bivio::UI::HTML::Widget::Image; use strict; use Bivio::Base 'HTMLWidget.ControlBase'; # C # # # # align : string [] # # How to align the image. The allowed (case # insensitive) values are defined in # L. # The value affects the C and C attributes of the C tag. # # alt : array_ref (required) # # alt : string (required) # # The verbatim text to use for I. No Facade.Text lookup is performed. # # alt_text : string (required) # # Text tag to use for C attribute of C tag. Tag will be prefixed with # C qualifier. Resultant text will be passed to # Lescape|Bivio::Util/"escape_html">. # # alt_text : array_ref (required) # # attributes : string [] # # Arbitrary HTML attributes to be applied to the begin tag. Must begin # with leading space. # # class : any [] # # The html CLASS for the table. If exists, then border is not # defaulted. # # hspace : int [0] # # HSPACE attribute value. # # border : int [0] # # Set to zero by default, so you rarely need to set this. # # height : int [src's height] # # The (constant) height of the image. Useful in combination with single pixel # clear gif for forcing dimensions of an area. Both B and # B must be set. # # For "real" gifs, the dimensions are extracted from the file. # # id : any [] # # The html ID attribute. # # src : array_ref (required) # # Image to use for C attribute of C tag. I's # get_widget_value returns a string, which is looked up via # L if it is a qualified name # or it is used verbatim if it is a URI. # # src : string # # Name of the L to use. # # width : int [src's width] # # See B. # # vspace : int [0] # # VSPACE attribute value. our($VERSION) = sprintf('%d.%02d', q$Revision: 2.14 $ =~ /\d+/g); my($_VS) = 'Bivio::UI::HTML::ViewShortcuts'; my($_OLD_HTML) = [qw(hspace vspace width height border align attributes)]; my($_A) = b_use('FacadeComponent.Align'); my($_I) = b_use('FacadeComponent.Icon'); my($_HTML) = b_use('Bivio.HTML'); sub control_on_render { # (self, any, string_ref) : undef # Render the image. my($self, $source, $buffer) = @_; my($src) = ${$self->render_attr('src', $source)}; my($src_is_uri) = $src =~ m{[/:]}; my($src_name) = $src_is_uri ? ($src =~ m{([^/]+)\.\w+$})[0] || '' : $src; my($b) = 'SUPER::control_on_render($source, \$b); $b .= qq{ class="$src_name"} if $b !~ /class=|id=/ && $_VS->vs_xhtml($source); $$buffer .= $b; my($alt) = $self->has_keys('alt') ? $self->render_simple_attr('alt', $source) : Bivio::UI::Text->get_from_source($source) ->unsafe_get_widget_value_by_name( 'Image_alt.' . (defined($self->unsafe_get('alt_text')) ? $self->render_simple_attr('alt_text', $source) : $src_name, ), $source, ); $alt = 'none' unless $alt; $$buffer .= ' alt="' . Bivio::HTML->escape_attr_value($alt) . '"'; my($a) = {map(($_ => $self->render_simple_attr($_, $source)), @$_OLD_HTML)}; $a->{border} ||= '0' unless $b =~ /class=|id=/; foreach my $k (qw(width height)) { $a->{$k} ||= ''; } $$buffer .= join( '', $_A->as_html(delete($a->{align})), delete($a->{attributes}), map((length($a->{$_}) ? qq{ $_="$a->{$_}"} : ()), sort(keys(%$a))), ) . ( $src_is_uri ? qq{ src="$src"} : defined($self->unsafe_get('width')) ? (' src="' . $_HTML->escape_attr_value( $_I->get_value($src, $source)->{uri}) . '"') : $_I->format_html($src, $source) ) . ' />'; return; } sub initialize { # (self) : undef # Initializes static information. In this case, prefix, alt, and # src, and have_size. my($self) = @_; $self->initialize_attr('src'); $self->map_invoke( unsafe_initialize_attr => [ @$_OLD_HTML, 'alt', 'alt_text', ] ); return shift->SUPER::initialize(@_); } sub internal_as_string { my($self) = @_; return ($self->unsafe_get('src')); } sub internal_new_args { return { alt_text => ref($_[2]) eq 'HASH' ? undef : [splice(@_, 2, 1)]->[0], %{shift->internal_compute_new_args([qw(src)], \@_)}, }; } 1;