| 1 | # Copyright 2008, 2009, 2010, 2011, 2012 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | |||
| 18 | # gravity up,down,centre | ||
| 19 | # stick to start/end if already there | ||
| 20 | # which of upper/lower to grow | ||
| 21 | # set_page_range wait until first size_allocate to decide vpp,ppv | ||
| 22 | # choice keep vpp same and expand page, or keep page and adjust vpp | ||
| 23 | # integer ppv | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | package App::Chart::Gtk2::AdjScale; | ||
| 28 | use 5.008; | ||
| 29 | use strict; | ||
| 30 | use warnings; | ||
| 31 | use Glib::Ex::FreezeNotify; | ||
| 32 | use Glib::Ex::SignalIds; | ||
| 33 | use Gtk2; | ||
| 34 | use Gtk2::Ex::AdjustmentBits 47; # v.47 for set_empty() | ||
| 35 | use POSIX (); | ||
| 36 | use Scalar::Util; | ||
| 37 | |||
| 38 | use App::Chart; | ||
| 39 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 40 | use App::Chart::Glib::Ex::TieWeakNotify; | ||
| 41 | |||
| 42 | use constant DEBUG => 0; | ||
| 43 | |||
| 44 | BEGIN { | ||
| 45 | Glib::Type->register_enum ('App::Chart::Gtk2::AdjScale::Gravity', | ||
| 46 | 'lower', | ||
| 47 | 'upper', | ||
| 48 | 'centre'); | ||
| 49 | } | ||
| 50 | |||
| 51 | use constant { | ||
| 52 | DEFAULT_GRAVITY => 'centre', | ||
| 53 | DEFAULT_ORIENTATION => 'horizontal', | ||
| 54 | }; | ||
| 55 | |||
| 56 | use Glib::Object::Subclass | ||
| 57 | 'Gtk2::Adjustment', | ||
| 58 | signals => { }, | ||
| 59 | properties => [Glib::ParamSpec->double | ||
| 60 | ('pixel-per-value', | ||
| 61 | 'pixel-per-value', | ||
| 62 | 'Blurb', | ||
| 63 | 0, POSIX::DBL_MAX(), 0, | ||
| 64 | Glib::G_PARAM_READWRITE), | ||
| 65 | |||
| 66 | Glib::ParamSpec->double | ||
| 67 | ('value-per-pixel', | ||
| 68 | 'value-per-pixel', | ||
| 69 | 'Blurb', | ||
| 70 | 0, POSIX::DBL_MAX(), 0, | ||
| 71 | Glib::G_PARAM_READWRITE), | ||
| 72 | |||
| 73 | Glib::ParamSpec->double | ||
| 74 | ('page-increment-fraction', | ||
| 75 | 'page-increment-fraction', | ||
| 76 | 'Blurb', | ||
| 77 | 0, POSIX::DBL_MAX(), 0.85, | ||
| 78 | Glib::G_PARAM_READWRITE), | ||
| 79 | Glib::ParamSpec->double | ||
| 80 | ('step-increment-fraction', | ||
| 81 | 'step-increment-fraction', | ||
| 82 | 'Blurb', | ||
| 83 | 0, POSIX::DBL_MAX(), 0.1, | ||
| 84 | Glib::G_PARAM_READWRITE), | ||
| 85 | |||
| 86 | Glib::ParamSpec->object | ||
| 87 | ('widget', | ||
| 88 | 'widget', | ||
| 89 | 'Blurb', | ||
| 90 | 'Gtk2::Widget', | ||
| 91 | Glib::G_PARAM_READWRITE), | ||
| 92 | |||
| 93 | Glib::ParamSpec->enum | ||
| 94 | ('orientation', | ||
| 95 | 'orientation', | ||
| 96 | 'Blurb', | ||
| 97 | 'Gtk2::Orientation', | ||
| 98 | DEFAULT_ORIENTATION, | ||
| 99 | Glib::G_PARAM_READWRITE), | ||
| 100 | |||
| 101 | Glib::ParamSpec->enum | ||
| 102 | ('gravity', | ||
| 103 | 'gravity', | ||
| 104 | 'Blurb', | ||
| 105 | 'App::Chart::Gtk2::AdjScale::Gravity', | ||
| 106 | DEFAULT_GRAVITY, | ||
| 107 | Glib::G_PARAM_READWRITE), | ||
| 108 | |||
| 109 | Glib::ParamSpec->boolean | ||
| 110 | ('inverted', | ||
| 111 | 'inverted', | ||
| 112 | 'Blurb', | ||
| 113 | 0, # default | ||
| 114 | Glib::G_PARAM_READWRITE), | ||
| 115 | |||
| 116 | Glib::ParamSpec->boolean | ||
| 117 | ('logarithmic', | ||
| 118 | 'logarithmic', | ||
| 119 | 'Blurb', | ||
| 120 | 0, # default | ||
| 121 | Glib::G_PARAM_READWRITE), | ||
| 122 | ]; | ||
| 123 | |||
| 124 | |||
| 125 | sub INIT_INSTANCE { | ||
| 126 | my ($self) = @_; | ||
| 127 | $self->{'value_per_pixel'} = 0; | ||
| 128 | $self->{'pixel_per_value'} = 0; | ||
| 129 | $self->{'gravity'} = DEFAULT_GRAVITY; | ||
| 130 | $self->{'orientation'} = DEFAULT_ORIENTATION; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub SET_PROPERTY { | ||
| 134 | my ($self, $pspec, $newval) = @_; | ||
| 135 | my $pname = $pspec->get_name; | ||
| 136 | if (DEBUG) { print "AdjScale set $pname $newval\n"; } | ||
| 137 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 138 | |||
| 139 | if ($pname eq 'value_per_pixel') { | ||
| 140 | $self->set_value_per_pixel ($newval); | ||
| 141 | |||
| 142 | } elsif ($pname eq 'pixel_per_value') { | ||
| 143 | $self->set_pixel_per_value ($newval); | ||
| 144 | |||
| 145 | } elsif ($pname eq 'widget') { | ||
| 146 | my $widget = $newval; | ||
| 147 | App::Chart::Glib::Ex::TieWeakNotify->set ($self, $pname, $widget); | ||
| 148 | $self->{'widget_ids'} = $widget && Glib::Ex::SignalIds->new | ||
| 149 | ($widget, | ||
| 150 | $widget->signal_connect (size_allocate => \&_do_size_allocate, | ||
| 151 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self))); | ||
| 152 | _update_page_size ($self); | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub set_page_range { | ||
| 157 | my ($self, $p_lo, $p_hi) = @_; | ||
| 158 | if (DEBUG) { print "AdjScale set_page_range $p_lo $p_hi\n"; } | ||
| 159 | |||
| 160 | my $page = $p_hi - $p_lo; | ||
| 161 | my $pixels = _widget_pixels ($self); | ||
| 162 | my $ppv = $self->{'pixel_per_value'} = ($page == 0 ? 0 : $pixels / $page); | ||
| 163 | $self->{'value_per_pixel'} = ($ppv == 0 ? 0 : 1.0 / $ppv); | ||
| 164 | if (DEBUG) { print "AdjScale set_page_range $p_lo, $p_hi ", | ||
| 165 | "is $page in pixels $pixels, ppv $ppv vpp ", | ||
| 166 | $self->{'value_per_pixel'},"\n"; } | ||
| 167 | |||
| 168 | Gtk2::Ex::AdjustmentBits::set_maybe | ||
| 169 | ($self, | ||
| 170 | lower => $p_lo, | ||
| 171 | upper => $p_hi, | ||
| 172 | page_size => $page, | ||
| 173 | page_increment => $self->get('page-increment-fraction') *$page, | ||
| 174 | step_increment => $self->get('step-increment-fraction') *$page, | ||
| 175 | value => $p_lo); | ||
| 176 | $self->notify ('pixel-per-value'); | ||
| 177 | $self->notify ('value-per-pixel'); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub set_value_per_pixel { | ||
| 181 | my ($self, $vpp) = @_; | ||
| 182 | if (DEBUG) { print "AdjScale set_value_per_pixel $vpp\n"; } | ||
| 183 | $self->{'value_per_pixel'} = $vpp; | ||
| 184 | $self->{'pixel_per_value'} = ($vpp == 0 ? 0 : 1.0 / $vpp); | ||
| 185 | _update_page_size ($self); | ||
| 186 | $self->notify ('value-per-pixel'); | ||
| 187 | $self->notify ('pixel-per-value'); | ||
| 188 | } | ||
| 189 | sub set_pixel_per_value { | ||
| 190 | my ($self, $ppv) = @_; | ||
| 191 | if (DEBUG) { print "AdjScale set_pixel_per_value $ppv\n"; } | ||
| 192 | $self->{'pixel_per_value'} = $ppv; | ||
| 193 | $self->{'value_per_pixel'} = ($ppv == 0 ? 0 : 1.0 / $ppv); | ||
| 194 | _update_page_size ($self); | ||
| 195 | $self->notify ('value-per-pixel'); | ||
| 196 | $self->notify ('pixel-per-value'); | ||
| 197 | } | ||
| 198 | sub set_value_range { | ||
| 199 | my ($self, $lo, $hi) = @_; | ||
| 200 | $self->value ($lo); | ||
| 201 | my $pixels = _widget_pixels ($self); | ||
| 202 | $self->set_pixel_per_value ($pixels / ($hi - $lo)); | ||
| 203 | $self->notify ('value'); | ||
| 204 | $self->value_changed; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub get_value_per_pixel { | ||
| 208 | my ($self) = @_; | ||
| 209 | return $self->{'value_per_pixel'}; | ||
| 210 | } | ||
| 211 | sub get_pixel_per_value { | ||
| 212 | my ($self) = @_; | ||
| 213 | return $self->{'pixel_per_value'}; | ||
| 214 | } | ||
| 215 | |||
| 216 | sub value_range { | ||
| 217 | my ($self) = @_; | ||
| 218 | my $value = $self->value; | ||
| 219 | return ($self->exp ($value), | ||
| 220 | $self->exp ($value + $self->page_size)); | ||
| 221 | } | ||
| 222 | sub value_range_inc { | ||
| 223 | my ($self) = @_; | ||
| 224 | my $value = $self->value; | ||
| 225 | return (POSIX::floor ($self->exp ($value)), | ||
| 226 | POSIX::ceil ($self->exp ($value + $self->page_size))); | ||
| 227 | } | ||
| 228 | |||
| 229 | sub value_to_pixel { | ||
| 230 | my ($self, $v) = @_; | ||
| 231 | my $value = $self->value; | ||
| 232 | my $ppv = $self->{'pixel_per_value'}; | ||
| 233 | if ($self->{'inverted'}) { | ||
| 234 | $ppv = -$ppv; | ||
| 235 | $value += $self->page_size - $self->{'value_per_pixel'}; | ||
| 236 | } | ||
| 237 | return POSIX::floor (($self->log($v) - $value) * $ppv); | ||
| 238 | } | ||
| 239 | sub value_to_pixel_proc { | ||
| 240 | my ($self) = @_; | ||
| 241 | my $value = $self->value; | ||
| 242 | my $ppv = $self->{'pixel_per_value'}; | ||
| 243 | if ($self->{'inverted'}) { | ||
| 244 | $ppv = -$ppv; | ||
| 245 | $value += $self->page_size - $self->{'value_per_pixel'}; | ||
| 246 | } | ||
| 247 | if ($self->{'logarithmic'}) { | ||
| 248 | return sub { | ||
| 249 | return POSIX::floor ((log($_[0]) - $value) * $ppv); | ||
| 250 | }; | ||
| 251 | } else { | ||
| 252 | return sub { | ||
| 253 | return POSIX::floor (($_[0] - $value) * $ppv); | ||
| 254 | }; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | sub pixel_to_value { | ||
| 259 | my ($self, $pixel) = @_; | ||
| 260 | my $vpp = $self->{'value_per_pixel'}; | ||
| 261 | my $base = 0; | ||
| 262 | if ($self->{'inverted'}) { | ||
| 263 | $vpp = -$vpp; | ||
| 264 | $base = _widget_pixels($self) - 1; | ||
| 265 | } | ||
| 266 | return $self->exp ($self->value + ($pixel - $base) * $vpp); | ||
| 267 | } | ||
| 268 | sub pixel_to_value_proc { | ||
| 269 | my ($self) = @_; | ||
| 270 | my $value = $self->value; | ||
| 271 | my $vpp = $self->{'value_per_pixel'}; | ||
| 272 | my $base = 0; | ||
| 273 | if ($self->{'inverted'}) { | ||
| 274 | $vpp = -$vpp; | ||
| 275 | $base = _widget_pixels($self) - 1; | ||
| 276 | } | ||
| 277 | if ($self->{'logarithmic'}) { | ||
| 278 | return sub { | ||
| 279 | return exp ($value + ($_[0] - $base) * $vpp); | ||
| 280 | }; | ||
| 281 | } else { | ||
| 282 | return sub { | ||
| 283 | return ($value + ($_[0] - $base) * $vpp); | ||
| 284 | }; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | sub exp { | ||
| 289 | my ($self, $x) = @_; | ||
| 290 | return ($self->{'logarithmic'} ? exp($x) : $x); | ||
| 291 | } | ||
| 292 | sub log { | ||
| 293 | my ($self, $x) = @_; | ||
| 294 | return ($self->{'logarithmic'} ? log($x) : $x); | ||
| 295 | } | ||
| 296 | sub exp_proc { | ||
| 297 | my ($self) = @_; | ||
| 298 | return ($self->{'logarithmic'} ? \&exp : \&identity); | ||
| 299 | } | ||
| 300 | sub log_proc { | ||
| 301 | my ($self) = @_; | ||
| 302 | return ($self->{'logarithmic'} ? \&log : \&identity); | ||
| 303 | } | ||
| 304 | sub identity { return $_[0]; } | ||
| 305 | |||
| 306 | # 'size-allocate' signal on widget | ||
| 307 | sub _do_size_allocate { | ||
| 308 | my ($widget, $alloc, $ref_weak_self) = @_; | ||
| 309 | my $self = $$ref_weak_self || return; | ||
| 310 | if (DEBUG) { print "AdjScale size_allocate\n"; } | ||
| 311 | _update_page_size ($self); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub _update_page_size { | ||
| 315 | my ($self) = @_; | ||
| 316 | if (DEBUG) { print " _update_page_size\n"; } | ||
| 317 | my %values; | ||
| 318 | |||
| 319 | # whether currently showing the end, or roughly so | ||
| 320 | my $upper = $self->upper; | ||
| 321 | my $at_end = ($self->value >= $upper - $self->page_size * 1.01); | ||
| 322 | if (DEBUG) { print " at_end ",$at_end?"yes":"no"," with upper $upper\n"; } | ||
| 323 | |||
| 324 | my $height = ($self->{'widget'} | ||
| 325 | ? $self->{'widget'}->allocation->height | ||
| 326 | : 0); | ||
| 327 | my $page = $height * $self->{'value_per_pixel'}; | ||
| 328 | $values{'page_size'} = $page; | ||
| 329 | $values{'page_increment'} = $self->get('page-increment-fraction') * $page; | ||
| 330 | $values{'step_increment'} = $self->get('step-increment-fraction') * $page; | ||
| 331 | if (DEBUG) { print " page $page on $height pixels and value_per_pixel ", | ||
| 332 | $self->{'value_per_pixel'},"\n"; } | ||
| 333 | |||
| 334 | # if upper-lower smaller than new page size then extend upper | ||
| 335 | my $lower = $self->lower; | ||
| 336 | if ($upper - $lower < $page) { | ||
| 337 | $upper = $lower + $page; | ||
| 338 | $values{'upper'} = $upper; | ||
| 339 | } | ||
| 340 | |||
| 341 | # if bigger page pushes value+page above upper then reduce value to max; | ||
| 342 | # if we were showing the end and a smaller page size means we no longer | ||
| 343 | # are then increase value to its max | ||
| 344 | my $max_value = $upper - $page; | ||
| 345 | if ($self->value > $max_value || $at_end) { | ||
| 346 | if (DEBUG) { print " bigger page pushes value down to $max_value\n"; } | ||
| 347 | $values{'value'} = $max_value; | ||
| 348 | } | ||
| 349 | |||
| 350 | if (DEBUG) { print " page $page", | ||
| 351 | " vpp ",$self->{'value_per_pixel'}, | ||
| 352 | " ppv ",$self->{'pixel_per_value'},"\n"; } | ||
| 353 | Gtk2::Ex::AdjustmentBits::set_maybe ($self, %values); | ||
| 354 | } | ||
| 355 | |||
| 356 | sub empty { | ||
| 357 | my ($self) = @_; | ||
| 358 | Gtk2::Ex::AdjustmentBits::set_empty($self); | ||
| 359 | } | ||
| 360 | sub is_empty { | ||
| 361 | my ($self) = @_; | ||
| 362 | return ($self->page_size == 0); | ||
| 363 | } | ||
| 364 | |||
| 365 | sub _widget_pixels { | ||
| 366 | my ($self) = @_; | ||
| 367 | my $widget = $self->{'widget'} || return 0; | ||
| 368 | my $alloc = $widget->allocation; | ||
| 369 | return ($self->{'orientation'} eq 'horizontal' | ||
| 370 | ? $alloc->width : $alloc->height); | ||
| 371 | } | ||
| 372 | |||
| 373 | # scroll by $count many steps | ||
| 374 | sub scroll_step { | ||
| 375 | my ($self, $count) = @_; | ||
| 376 | Gtk2::Ex::AdjustmentBits::set_maybe | ||
| 377 | ($self, | ||
| 378 | value => $self->value + $self->step_increment * $count); | ||
| 379 | } | ||
| 380 | |||
| 381 | 1; | ||
| 382 | __END__ |