| 1 | # ---------------------------------------------------------------------- | ||
| 2 | # Curses::UI::Progressbar | ||
| 3 | # | ||
| 4 | # (c) 2001-2002 by Maurice Makaay. All rights reserved. | ||
| 5 | # This file is part of Curses::UI. Curses::UI is free software. | ||
| 6 | # You can redistribute it and/or modify it under the same terms | ||
| 7 | # as perl itself. | ||
| 8 | # | ||
| 9 | # Currently maintained by Marcus Thiesen | ||
| 10 | # e-mail: marcus@cpan.thiesenweb.de | ||
| 11 | # ---------------------------------------------------------------------- | ||
| 12 | |||
| 13 | # TODO: fix dox | ||
| 14 | |||
| 15 | package Curses::UI::Progressbar; | ||
| 16 | |||
| 17 | use strict; | ||
| 18 | use Curses; | ||
| 19 | use Curses::UI::Common; | ||
| 20 | use Curses::UI::Widget; | ||
| 21 | |||
| 22 | use vars qw( | ||
| 23 | $VERSION | ||
| 24 | @ISA | ||
| 25 | ); | ||
| 26 | |||
| 27 | @ISA = qw( | ||
| 28 | Curses::UI::Widget | ||
| 29 | Curses::UI::Common | ||
| 30 | ); | ||
| 31 | |||
| 32 | $VERSION = '1.10'; | ||
| 33 | |||
| 34 | sub new () | ||
| 35 | { | ||
| 36 | my $class = shift; | ||
| 37 | |||
| 38 | my %userargs = @_; | ||
| 39 | keys_to_lowercase(\%userargs); | ||
| 40 | |||
| 41 | my %args = ( | ||
| 42 | -min => 0, # minimal value | ||
| 43 | -max => 100, # maximum value | ||
| 44 | -pos => 0, # the current position | ||
| 45 | -nopercentage => 0, # show the percentage or not? | ||
| 46 | -nocenterline => 0, # show the center line or not? | ||
| 47 | -showvalue => 0, # show value instead of percentage | ||
| 48 | -border => 1, | ||
| 49 | -fg => -1, | ||
| 50 | -bg => -1, | ||
| 51 | |||
| 52 | %userargs, | ||
| 53 | |||
| 54 | -focusable => 0, | ||
| 55 | -nocursor => 1, | ||
| 56 | ); | ||
| 57 | |||
| 58 | # Check that the lowest value comes first. | ||
| 59 | if ($args{-min} > $args{-max}) | ||
| 60 | { | ||
| 61 | my $tmp = $args{-min}; | ||
| 62 | $args{-min} = $args{-max}; | ||
| 63 | $args{-max} = $tmp; | ||
| 64 | } | ||
| 65 | |||
| 66 | my $height = height_by_windowscrheight(1, %args); | ||
| 67 | $args{-height} = $height; | ||
| 68 | |||
| 69 | my $this = $class->SUPER::new( %args ); | ||
| 70 | return $this; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub get() | ||
| 74 | { | ||
| 75 | my $this = shift; | ||
| 76 | return $this->{-pos}; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub pos(;$) | ||
| 80 | { | ||
| 81 | my $this = shift; | ||
| 82 | my $pos = shift || 0; | ||
| 83 | $this->{-pos} = $pos; | ||
| 84 | $this->intellidraw; | ||
| 85 | return $this; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub draw(;$) | ||
| 89 | { | ||
| 90 | my $this = shift; | ||
| 91 | my $no_doupdate = shift || 0; | ||
| 92 | |||
| 93 | # Draw the widget | ||
| 94 | $this->SUPER::draw(1) or return $this; | ||
| 95 | |||
| 96 | # Check bounds for the position. | ||
| 97 | $this->{-pos} = $this->{-max} if $this->{-pos} > $this->{-max}; | ||
| 98 | $this->{-pos} = $this->{-min} if $this->{-pos} < $this->{-min}; | ||
| 99 | |||
| 100 | if ($Curses::UI::color_support) { | ||
| 101 | my $co = $Curses::UI::color_object; | ||
| 102 | my $pair = $co->get_color_pair( | ||
| 103 | $this->{-fg}, | ||
| 104 | $this->{-bg}); | ||
| 105 | |||
| 106 | $this->{-canvasscr}->attron(COLOR_PAIR($pair)); | ||
| 107 | |||
| 108 | } | ||
| 109 | |||
| 110 | # Compute percentage | ||
| 111 | my $perc = ($this->{-pos}-$this->{-min}) | ||
| 112 | /($this->{-max}-$this->{-min})*100; | ||
| 113 | |||
| 114 | # Compute the number of blocks to draw. Only draw | ||
| 115 | # no blocks or all blocks if resp. the min. or the | ||
| 116 | # max. value is set. | ||
| 117 | my $blocks = int($perc * $this->canvaswidth / 100); | ||
| 118 | if ($blocks == 0 and | ||
| 119 | $this->{-pos} != $this->{-min}) { $blocks++ } | ||
| 120 | if ($blocks == $this->canvaswidth and | ||
| 121 | $this->{-pos} != $this->{-max}) { $blocks-- } | ||
| 122 | |||
| 123 | # Draw center line | ||
| 124 | $this->{-canvasscr}->addstr(0, 0, "-"x$this->canvaswidth) | ||
| 125 | unless $this->{-nocenterline}; | ||
| 126 | |||
| 127 | # Draw blocks. | ||
| 128 | $this->{-canvasscr}->attron(A_REVERSE); | ||
| 129 | $this->{-canvasscr}->addstr(0, 0, " "x$blocks); | ||
| 130 | $this->{-canvasscr}->attroff(A_REVERSE); | ||
| 131 | |||
| 132 | # Draw percentage | ||
| 133 | if (not $this->{-nopercentage} or $this->{-showvalue}) | ||
| 134 | { | ||
| 135 | my $str; | ||
| 136 | if ($this->{-showvalue}) { | ||
| 137 | $str = " $this->{-pos} "; | ||
| 138 | } else { | ||
| 139 | $str = " " . int($perc) . "% "; | ||
| 140 | } | ||
| 141 | |||
| 142 | my $len = length($str); | ||
| 143 | my $xpos = int(($this->canvaswidth - $len)/2); | ||
| 144 | my $revlen = $blocks - $xpos; | ||
| 145 | $revlen = 0 if $revlen < 0; | ||
| 146 | $revlen = $len if $revlen > $len; | ||
| 147 | my $rev = substr($str, 0, $revlen); | ||
| 148 | my $norev = substr($str, $revlen, $len-$revlen); | ||
| 149 | $this->{-canvasscr}->attron(A_REVERSE); | ||
| 150 | $this->{-canvasscr}->addstr(0, $xpos, $rev); | ||
| 151 | $this->{-canvasscr}->attroff(A_REVERSE); | ||
| 152 | $this->{-canvasscr}->addstr(0, $xpos+$revlen, $norev); | ||
| 153 | } | ||
| 154 | |||
| 155 | $this->{-canvasscr}->move(0,$this->canvaswidth-1); | ||
| 156 | |||
| 157 | $this->{-canvasscr}->noutrefresh(); | ||
| 158 | doupdate() unless $no_doupdate; | ||
| 159 | |||
| 160 | return $this; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | 1; | ||
| 165 | |||
| 166 | |||
| 167 | =pod | ||
| 168 | |||
| 169 | =head1 NAME | ||
| 170 | |||
| 171 | Curses::UI::Progressbar - Create and manipulate progressbar widgets | ||
| 172 | |||
| 173 | =head1 CLASS HIERARCHY | ||
| 174 | |||
| 175 | Curses::UI::Widget | ||
| 176 | | | ||
| 177 | +----Curses::UI::Progressbar | ||
| 178 | |||
| 179 | |||
| 180 | =head1 SYNOPSIS | ||
| 181 | |||
| 182 | use Curses::UI; | ||
| 183 | my $cui = new Curses::UI; | ||
| 184 | my $win = $cui->add('window_id', 'Window'); | ||
| 185 | |||
| 186 | my $progressbar = $win->add( | ||
| 187 | 'myprogressbar', 'Progressbar', | ||
| 188 | -max => 250, | ||
| 189 | -pos => 42, | ||
| 190 | ); | ||
| 191 | |||
| 192 | $progressbar->draw; | ||
| 193 | |||
| 194 | |||
| 195 | =head1 DESCRIPTION | ||
| 196 | |||
| 197 | Curses::UI::Progressbar is a widget that can be used to | ||
| 198 | provide some sort of progress information to the user | ||
| 199 | of your program. The progressbar looks like this: | ||
| 200 | |||
| 201 | +------------------------------------------+ | ||
| 202 | |||||||||---------- 14% ------------------ | | ||
| 203 | +------------------------------------------+ | ||
| 204 | |||
| 205 | See exampes/demo-Curses::UI::Progressbar in the distribution | ||
| 206 | for a short demo. | ||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | =head1 STANDARD OPTIONS | ||
| 211 | |||
| 212 | B<-parent>, B<-x>, B<-y>, B<-width>, B<-height>, | ||
| 213 | B<-pad>, B<-padleft>, B<-padright>, B<-padtop>, B<-padbottom>, | ||
| 214 | B<-ipad>, B<-ipadleft>, B<-ipadright>, B<-ipadtop>, B<-ipadbottom>, | ||
| 215 | B<-title>, B<-titlefullwidth>, B<-titlereverse> | ||
| 216 | |||
| 217 | For an explanation of these standard options, see | ||
| 218 | L<Curses::UI::Widget|Curses::UI::Widget>. | ||
| 219 | |||
| 220 | |||
| 221 | |||
| 222 | |||
| 223 | =head1 WIDGET-SPECIFIC OPTIONS | ||
| 224 | |||
| 225 | =over 4 | ||
| 226 | |||
| 227 | =item * B<-min> < VALUE > | ||
| 228 | |||
| 229 | This opion sets the minimum value for the progress bar. | ||
| 230 | Default is 0. | ||
| 231 | |||
| 232 | =item * B<-max> < VALUE > | ||
| 233 | |||
| 234 | This opion sets the maximum value for the progress bar. | ||
| 235 | |||
| 236 | =item * B<-pos> < VALUE > | ||
| 237 | |||
| 238 | This option sets the startposition for the progress | ||
| 239 | bar. | ||
| 240 | |||
| 241 | =item * B<-nopercentage> < BOOLEAN > | ||
| 242 | |||
| 243 | This option controls if a percentage indicator should | ||
| 244 | be drawn in the widget. The default for the BOOLEAN | ||
| 245 | value is false, so a percentage incdicator will be drawn. | ||
| 246 | |||
| 247 | =item * B<-showvalue> < BOOLEAN > | ||
| 248 | |||
| 249 | If this option is set to a true value, the current | ||
| 250 | position value will be drawn in the widget. | ||
| 251 | |||
| 252 | =item * B<-nocenterline> < BOOLEAN > | ||
| 253 | |||
| 254 | This option controls if a horizontal line should | ||
| 255 | be drawn in the widget. The default for the BOOLEAN | ||
| 256 | value is false, so a horizontal line will be drawn. | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | |||
| 261 | |||
| 262 | |||
| 263 | =head1 METHODS | ||
| 264 | |||
| 265 | =over 4 | ||
| 266 | |||
| 267 | =item * B<new> ( OPTIONS ) | ||
| 268 | |||
| 269 | =item * B<layout> ( ) | ||
| 270 | |||
| 271 | =item * B<draw> ( BOOLEAN ) | ||
| 272 | |||
| 273 | =item * B<intellidraw> ( ) | ||
| 274 | |||
| 275 | =item * B<focus> ( ) | ||
| 276 | |||
| 277 | These are standard methods. See L<Curses::UI::Widget|Curses::UI::Widget> | ||
| 278 | for an explanation of these. | ||
| 279 | |||
| 280 | =item * B<get> ( ) | ||
| 281 | |||
| 282 | This method will return the current B<-pos> value of the widget. | ||
| 283 | |||
| 284 | =item * B<pos> ( VALUE ) | ||
| 285 | |||
| 286 | This method will set the B<-pos> value of the widget to SCALAR. | ||
| 287 | |||
| 288 | =back | ||
| 289 | |||
| 290 | |||
| 291 | |||
| 292 | |||
| 293 | =head1 DEFAULT BINDINGS | ||
| 294 | |||
| 295 | Since a Progressbar is a non-interacting widget, it does not have | ||
| 296 | any bindings. | ||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | |||
| 302 | =head1 SEE ALSO | ||
| 303 | |||
| 304 | L<Curses::UI|Curses::UI>, | ||
| 305 | L<Curses::UI::Widget|Curses::UI::Widget>, | ||
| 306 | L<Curses::UI::Common|Curses::UI::Common> | ||
| 307 | |||
| 308 | |||
| 309 | |||
| 310 | |||
| 311 | =head1 AUTHOR | ||
| 312 | |||
| 313 | Copyright (c) 2001-2002 Maurice Makaay. All rights reserved. | ||
| 314 | |||
| 315 | Maintained by Marcus Thiesen (marcus@cpan.thiesenweb.de) | ||
| 316 | |||
| 317 | |||
| 318 | This package is free software and is provided "as is" without express | ||
| 319 | or implied warranty. It may be used, redistributed and/or modified | ||
| 320 | under the same terms as perl itself. | ||
| 321 |