| 1 | package Tk::ListBrowser::SideColumn; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | Tk::ListBrowser::SideColumn - Side columns for Tk::ListBrowser. | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw ($VERSION); | ||
| 12 | use Carp; | ||
| 13 | $VERSION = 0.15; | ||
| 14 | |||
| 15 | use base qw(Tk::ListBrowser::BaseItem); | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | my $column = $listbrowser->columnAdd($columnyname, @options); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | Inherits L<Tk::ListBrowser::BaseItem>. | ||
| 24 | |||
| 25 | This module creates an object that holds all information of every entry. | ||
| 26 | You will never need to create an side column object yourself. | ||
| 27 | |||
| 28 | Besides the options in it's parent, you can use the I<-sortcase>, | ||
| 29 | I<-sortfield>,and I<-sortnumerical> options. | ||
| 30 | |||
| 31 | Furthermore you can set the I<-cellwidth>, I<-imagecellwidth> and I<-textcellwidth> options. | ||
| 32 | The height options are not available since the height is determined by the main entry. | ||
| 33 | |||
| 34 | There is also the I<-drawrect> option. By default it is set to 0. If you set it the column will | ||
| 35 | draw a background rectangle in the background color. Do not use this in tree mode. It will clutter | ||
| 36 | your display when activating indicators. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub new { | ||
| 41 | my $class = shift; | ||
| 42 | |||
| 43 | |||
| 44 | my $self = $class->SUPER::new(@_); | ||
| 45 | |||
| 46 | $self->filterfield('text') unless defined $self->filterfield; | ||
| 47 | $self->itemtype('text') unless defined $self->itemtype; | ||
| 48 | $self->drawrect(1) unless defined $self->drawrect; | ||
| 49 | $self->sortcase('') unless defined $self->sortcase; | ||
| 50 | $self->sortfield('text') unless defined $self->sortfield; | ||
| 51 | $self->sortnumerical('') unless defined $self->sortnumerical; | ||
| 52 | $self->cellimagewidth(0); | ||
| 53 | $self->celltextwidth(0); | ||
| 54 | $self->cellwidth(100); | ||
| 55 | $self->{VALUES} = {}; | ||
| 56 | |||
| 57 | return $self | ||
| 58 | } | ||
| 59 | |||
| 60 | sub cellimagewidth { | ||
| 61 | my $self = shift; | ||
| 62 | $self->{IMAGEWIDTH} = shift if @_; | ||
| 63 | return $self->{IMAGEWIDTH} | ||
| 64 | } | ||
| 65 | |||
| 66 | sub celltextwidth { | ||
| 67 | my $self = shift; | ||
| 68 | $self->{TEXTWIDTH} = shift if @_; | ||
| 69 | return $self->{TEXTWIDTH} | ||
| 70 | } | ||
| 71 | |||
| 72 | sub cellSize { | ||
| 73 | my $self = shift; | ||
| 74 | |||
| 75 | my $cellwidth = 0; | ||
| 76 | my $imagewidth = 0; | ||
| 77 | my $textwidth = 0; | ||
| 78 | |||
| 79 | my $values = $self->{VALUES}; | ||
| 80 | for (keys %$values) { | ||
| 81 | my $item = $values->{$_}; | ||
| 82 | my ($cw, $ch, $iw, $ih, $tw, $th) = $item->requestedSize; | ||
| 83 | $imagewidth = $iw if $iw > $imagewidth; | ||
| 84 | $textwidth = $tw if $tw > $textwidth; | ||
| 85 | my $itemtype = $self->cget('-itemtype'); | ||
| 86 | if ($itemtype eq 'image') { | ||
| 87 | $cellwidth = $imagewidth; | ||
| 88 | } elsif ($itemtype eq 'text') { | ||
| 89 | $cellwidth = $textwidth; | ||
| 90 | } else { | ||
| 91 | my $textside = $self->cget('-textside'); | ||
| 92 | if (($textside eq 'top') or ($textside eq 'bottom')) { | ||
| 93 | $cellwidth = $imagewidth; | ||
| 94 | $cellwidth = $textwidth if $textwidth > $cellwidth; | ||
| 95 | $cellwidth = $cellwidth; | ||
| 96 | } elsif (($textside eq 'left') or ($textside eq 'right')) { | ||
| 97 | $cellwidth = $imagewidth + $textwidth; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | $self->cellwidth($cellwidth); | ||
| 102 | $self->cellimagewidth($imagewidth); | ||
| 103 | $self->celltextwidth($textwidth); | ||
| 104 | return $cellwidth; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub cellwidth { | ||
| 108 | my $self = shift; | ||
| 109 | $self->{CELLWIDTH} = shift if @_; | ||
| 110 | my $fw = $self->forceWidth; | ||
| 111 | return $fw if defined $fw; | ||
| 112 | return $self->{CELLWIDTH} | ||
| 113 | } | ||
| 114 | |||
| 115 | sub clear { | ||
| 116 | my $self = shift; | ||
| 117 | $self->SUPER::clear; | ||
| 118 | my @items = $self->itemList; | ||
| 119 | for (@items) { $self->itemGet($_)->clear } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub draw { | ||
| 123 | my $self = shift; | ||
| 124 | return unless $self->drawrect; | ||
| 125 | my $c = $self->Subwidget('Canvas'); | ||
| 126 | my @region = $self->region; | ||
| 127 | my $rtag = $c->createRectangle($self->region, | ||
| 128 | -fill => $self->background, | ||
| 129 | -outline => undef, | ||
| 130 | -tags => [$self->name], | ||
| 131 | ); | ||
| 132 | $self->crect($rtag); | ||
| 133 | } | ||
| 134 | |||
| 135 | sub drawrect { | ||
| 136 | my $self = shift; | ||
| 137 | $self->{DRAWRECT} = shift if @_; | ||
| 138 | return $self->{DRAWRECT} | ||
| 139 | } | ||
| 140 | |||
| 141 | sub filterfield { | ||
| 142 | my $self = shift; | ||
| 143 | $self->{FILTERFIELD} = shift if @_; | ||
| 144 | return $self->{FILTERFIELD} | ||
| 145 | } | ||
| 146 | |||
| 147 | sub forceWidth { | ||
| 148 | my $self = shift; | ||
| 149 | $self->{FORCEWIDTH} = shift if @_; | ||
| 150 | return $self->{FORCEWIDTH} | ||
| 151 | } | ||
| 152 | |||
| 153 | sub header { | ||
| 154 | my $self = shift; | ||
| 155 | $self->{HEADER} = shift if @_; | ||
| 156 | return $self->{HEADER} | ||
| 157 | } | ||
| 158 | |||
| 159 | sub itemAdd { | ||
| 160 | my ($self, $entry, $item) = @_; | ||
| 161 | my $vh = $self->{VALUES}; | ||
| 162 | $vh->{$entry} = $item; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub itemExists { | ||
| 166 | my ($self, $entry) = @_; | ||
| 167 | my $vh = $self->{VALUES}; | ||
| 168 | return exists $vh->{$entry}; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub itemGet { | ||
| 172 | my ($self, $entry) = @_; | ||
| 173 | my $vh = $self->{VALUES}; | ||
| 174 | return $vh->{$entry}; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub itemList { | ||
| 178 | my ($self, $entry) = @_; | ||
| 179 | my $vh = $self->{VALUES}; | ||
| 180 | return keys %$vh; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub itemRemove { | ||
| 184 | my ($self, $entry) = @_; | ||
| 185 | my $vh = $self->{VALUES}; | ||
| 186 | my $i = $vh->{$entry}; | ||
| 187 | return unless defined $i; | ||
| 188 | $i->clear; | ||
| 189 | delete $vh->{$entry}; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub sortcase{ | ||
| 193 | my $self = shift; | ||
| 194 | $self->{SORTCASE} = shift if @_; | ||
| 195 | return $self->{SORTCASE} | ||
| 196 | } | ||
| 197 | |||
| 198 | sub sortfield{ | ||
| 199 | my $self = shift; | ||
| 200 | $self->{SORTFIELD} = shift if @_; | ||
| 201 | return $self->{SORTFIELD} | ||
| 202 | } | ||
| 203 | |||
| 204 | sub sortnumerical{ | ||
| 205 | my $self = shift; | ||
| 206 | $self->{SORTNUMERICAL} = shift if @_; | ||
| 207 | return $self->{SORTNUMERICAL} | ||
| 208 | } | ||
| 209 | |||
| 210 | =head1 LICENSE | ||
| 211 | |||
| 212 | Same as Perl. | ||
| 213 | |||
| 214 | =head1 AUTHOR | ||
| 215 | |||
| 216 | Hans Jeuken (hanje at cpan dot org) | ||
| 217 | |||
| 218 | =head1 BUGS AND CAVEATS | ||
| 219 | |||
| 220 | If you find any bugs, please report them here: L<https://github.com/haje61/Tk-ListBrowser/issues>. | ||
| 221 | |||
| 222 | =head1 SEE ALSO | ||
| 223 | |||
| 224 | =over 4 | ||
| 225 | |||
| 226 | =item L<Tk::ListBrowser::BaseItem> | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | =cut | ||
| 231 |