| 1 | package Gtk3::ImageView; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | no if $] >= 5.018, warnings => 'experimental::smartmatch'; | ||
| 6 | use Cairo; | ||
| 7 | use Glib qw(TRUE FALSE); # To get TRUE and FALSE | ||
| 8 | use Gtk3; | ||
| 9 | use Gtk3::ImageView::Tool; | ||
| 10 | use Gtk3::ImageView::Tool::Dragger; | ||
| 11 | use Gtk3::ImageView::Tool::Selector; | ||
| 12 | use List::Util qw(min); | ||
| 13 | use Scalar::Util qw(blessed); | ||
| 14 | use Carp; | ||
| 15 | use Readonly; | ||
| 16 | Readonly my $MAX_ZOOM => 100; | ||
| 17 | |||
| 18 | our $VERSION = '12'; | ||
| 19 | |||
| 20 | use Glib::Object::Subclass Gtk3::DrawingArea::, signals => { | ||
| 21 | 'zoom-changed' => { | ||
| 22 | param_types => ['Glib::Float'], # new zoom | ||
| 23 | }, | ||
| 24 | 'offset-changed' => { | ||
| 25 | param_types => [ 'Glib::Int', 'Glib::Int' ], # new offset | ||
| 26 | }, | ||
| 27 | 'selection-changed' => { | ||
| 28 | param_types => ['Glib::Scalar'], # Gdk::Rectangle of selection area | ||
| 29 | }, | ||
| 30 | 'tool-changed' => { | ||
| 31 | param_types => ['Glib::Scalar'], # new Gtk3::ImageView::Tool | ||
| 32 | }, | ||
| 33 | 'dnd-start' => { | ||
| 34 | param_types => [ | ||
| 35 | 'Glib::Float', # x | ||
| 36 | 'Glib::Float', # y | ||
| 37 | 'Glib::UInt', # button | ||
| 38 | ], | ||
| 39 | return_type => 'Glib::Boolean', | ||
| 40 | flags => ['run-last'], | ||
| 41 | } | ||
| 42 | }, | ||
| 43 | properties => [ | ||
| 44 | Glib::ParamSpec->object( | ||
| 45 | 'pixbuf', # name | ||
| 46 | 'pixbuf', # nickname | ||
| 47 | 'Gtk3::Gdk::Pixbuf to be shown', # blurb | ||
| 48 | 'Gtk3::Gdk::Pixbuf', | ||
| 49 | [qw/readable writable/] # flags | ||
| 50 | ), | ||
| 51 | Glib::ParamSpec->scalar( | ||
| 52 | 'offset', # name | ||
| 53 | 'Image offset', # nick | ||
| 54 | 'Gdk::Rectangle hash of x, y', # blurb | ||
| 55 | [qw/readable writable/] # flags | ||
| 56 | ), | ||
| 57 | Glib::ParamSpec->float( | ||
| 58 | 'zoom', # name | ||
| 59 | 'zoom', # nick | ||
| 60 | 'zoom level', # blurb | ||
| 61 | 0.001, # minimum | ||
| 62 | 100.0, # maximum | ||
| 63 | 1.0, # default_value | ||
| 64 | [qw/readable writable/] # flags | ||
| 65 | ), | ||
| 66 | Glib::ParamSpec->float( | ||
| 67 | 'zoom-step', # name | ||
| 68 | 'Zoom step', # nick | ||
| 69 | 'Zoom coefficient for every scrolling step', # blurb | ||
| 70 | 1.0, # minimum | ||
| 71 | 10.0, # maximum | ||
| 72 | 2.0, # default_value | ||
| 73 | [qw/readable writable/] # flags | ||
| 74 | ), | ||
| 75 | Glib::ParamSpec->float( | ||
| 76 | 'resolution-ratio', # name | ||
| 77 | 'resolution-ratio', # nick | ||
| 78 | 'Ratio of x-resolution/y-resolution', # blurb | ||
| 79 | 0.0001, # minimum | ||
| 80 | 1000.0, # maximum | ||
| 81 | 1.0, # default_value | ||
| 82 | [qw/readable writable/] # flags | ||
| 83 | ), | ||
| 84 | Glib::ParamSpec->scalar( | ||
| 85 | 'tool', # name | ||
| 86 | 'tool', # nickname | ||
| 87 | 'Active Gtk3::ImageView::Tool', # blurb | ||
| 88 | [qw/readable writable/] # flags | ||
| 89 | ), | ||
| 90 | Glib::ParamSpec->scalar( | ||
| 91 | 'selection', # name | ||
| 92 | 'Selection', # nick | ||
| 93 | 'Gdk::Rectangle hash of selected region', # blurb | ||
| 94 | [qw/readable writable/] # flags | ||
| 95 | ), | ||
| 96 | Glib::ParamSpec->scalar( | ||
| 97 | 'selection-float', # name | ||
| 98 | 'Selection float', # nick | ||
| 99 | 'Gdk::Rectangle hash of selected region (floating point)', # blurb | ||
| 100 | [qw/readable writable/] # flags | ||
| 101 | ), | ||
| 102 | Glib::ParamSpec->boolean( | ||
| 103 | 'zoom-to-fit', # name | ||
| 104 | 'Zoom to fit', # nickname | ||
| 105 | 'Whether the zoom factor is automatically calculated to fit the window' | ||
| 106 | , # blurb | ||
| 107 | TRUE, # default | ||
| 108 | [qw/readable writable/] # flags | ||
| 109 | ), | ||
| 110 | Glib::ParamSpec->float( | ||
| 111 | 'zoom-to-fit-limit', # name | ||
| 112 | 'Zoom to fit limit', # nickname | ||
| 113 | 'When zooming automatically, don\'t zoom more than this', # blurb | ||
| 114 | 0.0001, # minimum | ||
| 115 | 100.0, # maximum | ||
| 116 | 100.0, # default_value | ||
| 117 | [qw/readable writable/] # flags | ||
| 118 | ), | ||
| 119 | Glib::ParamSpec->string( | ||
| 120 | 'interpolation', # name | ||
| 121 | 'interpolation', # nick | ||
| 122 | 'Interpolation method to use, from Cairo::Filter', # blurb | ||
| 123 | 'good', # default | ||
| 124 | [qw/readable writable/], # flags | ||
| 125 | ), | ||
| 126 | ]; | ||
| 127 | |||
| 128 | sub INIT_INSTANCE { | ||
| 129 | my $self = shift; | ||
| 130 | $self->signal_connect( draw => \&_draw ); | ||
| 131 | $self->signal_connect( 'button-press-event' => \&_button_pressed ); | ||
| 132 | $self->signal_connect( 'button-release-event' => \&_button_released ); | ||
| 133 | $self->signal_connect( 'motion-notify-event' => \&_motion ); | ||
| 134 | $self->signal_connect( 'scroll-event' => \&_scroll ); | ||
| 135 | $self->signal_connect( configure_event => \&_configure_event ); | ||
| 136 | $self->set_app_paintable(TRUE); | ||
| 137 | |||
| 138 | if ( | ||
| 139 | $Glib::Object::Introspection::VERSION < 0.043 ## no critic (ProhibitMagicNumbers) | ||
| 140 | ) | ||
| 141 | { | ||
| 142 | $self->add_events( | ||
| 143 | ${ Gtk3::Gdk::EventMask->new(qw/exposure-mask/) } | | ||
| 144 | ${ Gtk3::Gdk::EventMask->new(qw/button-press-mask/) } | | ||
| 145 | ${ Gtk3::Gdk::EventMask->new(qw/button-release-mask/) } | | ||
| 146 | ${ Gtk3::Gdk::EventMask->new(qw/pointer-motion-mask/) } | | ||
| 147 | ${ Gtk3::Gdk::EventMask->new(qw/scroll-mask/) } ); | ||
| 148 | } | ||
| 149 | else { | ||
| 150 | $self->add_events( | ||
| 151 | Glib::Object::Introspection->convert_sv_to_flags( | ||
| 152 | 'Gtk3::Gdk::EventMask', 'exposure-mask' ) | | ||
| 153 | Glib::Object::Introspection->convert_sv_to_flags( | ||
| 154 | 'Gtk3::Gdk::EventMask', 'button-press-mask' ) | | ||
| 155 | Glib::Object::Introspection->convert_sv_to_flags( | ||
| 156 | 'Gtk3::Gdk::EventMask', 'button-release-mask' ) | | ||
| 157 | Glib::Object::Introspection->convert_sv_to_flags( | ||
| 158 | 'Gtk3::Gdk::EventMask', 'pointer-motion-mask' ) | | ||
| 159 | Glib::Object::Introspection->convert_sv_to_flags( | ||
| 160 | 'Gtk3::Gdk::EventMask', 'scroll-mask' | ||
| 161 | ) | ||
| 162 | ); | ||
| 163 | } | ||
| 164 | $self->set_tool( Gtk3::ImageView::Tool::Dragger->new($self) ); | ||
| 165 | $self->set_redraw_on_allocate(FALSE); | ||
| 166 | return $self; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub SET_PROPERTY { | ||
| 170 | my ( $self, $pspec, $newval ) = @_; | ||
| 171 | my $name = $pspec->get_name; | ||
| 172 | my $oldval = $self->get($name); | ||
| 173 | my $invalidate = FALSE; | ||
| 174 | if ( ( defined $newval and defined $oldval and $newval ne $oldval ) | ||
| 175 | or ( defined $newval xor defined $oldval ) ) | ||
| 176 | { | ||
| 177 | if ( $name eq 'pixbuf' ) { | ||
| 178 | $self->{$name} = $newval; | ||
| 179 | $invalidate = TRUE; | ||
| 180 | } | ||
| 181 | elsif ( $name eq 'zoom' ) { | ||
| 182 | $self->{$name} = $newval; | ||
| 183 | $self->signal_emit( 'zoom-changed', $newval ); | ||
| 184 | $invalidate = TRUE; | ||
| 185 | } | ||
| 186 | elsif ( $name eq 'offset' ) { | ||
| 187 | if ( ( defined $newval xor defined $oldval ) | ||
| 188 | or $oldval->{x} != $newval->{x} | ||
| 189 | or $oldval->{y} != $newval->{y} ) | ||
| 190 | { | ||
| 191 | $self->{$name} = $newval; | ||
| 192 | $self->signal_emit( 'offset-changed', $newval->{x}, | ||
| 193 | $newval->{y} ); | ||
| 194 | $invalidate = TRUE; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | elsif ( $name eq 'resolution_ratio' ) { | ||
| 198 | $self->{$name} = $newval; | ||
| 199 | $invalidate = TRUE; | ||
| 200 | } | ||
| 201 | elsif ( $name eq 'interpolation' ) { | ||
| 202 | $self->{$name} = $newval; | ||
| 203 | $invalidate = TRUE; | ||
| 204 | } | ||
| 205 | elsif ( $name eq 'selection' ) { | ||
| 206 | if ( _selections_nequal( $oldval, $newval ) ) { | ||
| 207 | $self->{$name} = $newval; | ||
| 208 | $self->{selection_float} = $newval; | ||
| 209 | $invalidate = TRUE; | ||
| 210 | $self->signal_emit( 'selection-changed', $newval ); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | elsif ( $name eq 'selection_float' ) { | ||
| 214 | if ( _selections_nequal( $oldval, $newval ) ) { | ||
| 215 | $self->{$name} = $newval; | ||
| 216 | my $x1 = int( $newval->{x} + 0.5 ); | ||
| 217 | my $y1 = int( $newval->{y} + 0.5 ); | ||
| 218 | my $x2 = int( $newval->{x} + $newval->{width} + 0.5 ); | ||
| 219 | my $y2 = int( $newval->{y} + $newval->{height} + 0.5 ); | ||
| 220 | $self->{selection} = { | ||
| 221 | x => $x1, | ||
| 222 | y => $y1, | ||
| 223 | width => $x2 - $x1, | ||
| 224 | height => $y2 - $y1, | ||
| 225 | }; | ||
| 226 | |||
| 227 | # Even for the float selection, w/h should be int; otherwise dragging the selection looks funny | ||
| 228 | $self->{$name}->{width} = $x2 - $x1; | ||
| 229 | $self->{$name}->{height} = $y2 - $y1; | ||
| 230 | $invalidate = TRUE; | ||
| 231 | $self->signal_emit( 'selection-changed', $newval ); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | elsif ( $name eq 'tool' ) { | ||
| 235 | $self->{$name} = $newval; | ||
| 236 | if ( defined $self->get_selection ) { | ||
| 237 | $invalidate = TRUE; | ||
| 238 | } | ||
| 239 | $self->signal_emit( 'tool-changed', $newval ); | ||
| 240 | } | ||
| 241 | else { | ||
| 242 | $self->{$name} = $newval; | ||
| 243 | |||
| 244 | # $self->SUPER::SET_PROPERTY( $pspec, $newval ); | ||
| 245 | } | ||
| 246 | if ($invalidate) { | ||
| 247 | $self->queue_draw(); | ||
| 248 | } | ||
| 249 | } | ||
| 250 | return; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub _selections_nequal { | ||
| 254 | my ( $oldval, $newval ) = @_; | ||
| 255 | return ( | ||
| 256 | ( defined $newval xor defined $oldval ) | ||
| 257 | or $oldval->{x} != $newval->{x} | ||
| 258 | or $oldval->{y} != $newval->{y} | ||
| 259 | or $oldval->{width} != $newval->{width} | ||
| 260 | or $oldval->{height} != $newval->{height} | ||
| 261 | ); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub set_pixbuf { | ||
| 265 | my ( $self, $pixbuf, $zoom_to_fit ) = @_; | ||
| 266 | $self->set( 'pixbuf', $pixbuf ); | ||
| 267 | $self->set_zoom_to_fit($zoom_to_fit); | ||
| 268 | if ( not $zoom_to_fit ) { | ||
| 269 | $self->set_offset( 0, 0 ); | ||
| 270 | } | ||
| 271 | return; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub get_pixbuf { | ||
| 275 | my ($self) = @_; | ||
| 276 | return $self->get('pixbuf'); | ||
| 277 | } | ||
| 278 | |||
| 279 | sub get_pixbuf_size { | ||
| 280 | my ($self) = @_; | ||
| 281 | my $pixbuf = $self->get_pixbuf; | ||
| 282 | if ( defined $pixbuf ) { | ||
| 283 | return { width => $pixbuf->get_width, height => $pixbuf->get_height }; | ||
| 284 | } | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | sub _button_pressed { | ||
| 289 | my ( $self, $event ) = @_; | ||
| 290 | return $self->get_tool->button_pressed($event); | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _button_released { | ||
| 294 | my ( $self, $event ) = @_; | ||
| 295 | $self->get_tool->button_released($event); | ||
| 296 | return; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub _motion { | ||
| 300 | my ( $self, $event ) = @_; | ||
| 301 | $self->update_cursor( $event->x, $event->y ); | ||
| 302 | $self->get_tool->motion($event); | ||
| 303 | return; | ||
| 304 | } | ||
| 305 | |||
| 306 | sub _scroll { | ||
| 307 | my ( $self, $event ) = @_; | ||
| 308 | my ( $center_x, $center_y ) = | ||
| 309 | $self->to_image_coords( $event->x, $event->y ); | ||
| 310 | my $zoom; | ||
| 311 | $self->set_zoom_to_fit(FALSE); | ||
| 312 | if ( $event->direction eq 'up' ) { | ||
| 313 | $zoom = $self->get_zoom * $self->get('zoom-step'); | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | $zoom = $self->get_zoom / $self->get('zoom-step'); | ||
| 317 | } | ||
| 318 | $self->_set_zoom_with_center( $zoom, $center_x, $center_y ); | ||
| 319 | return; | ||
| 320 | } | ||
| 321 | |||
| 322 | sub _draw { | ||
| 323 | my ( $self, $context ) = @_; | ||
| 324 | my $allocation = $self->get_allocation; | ||
| 325 | my $style = $self->get_style_context; | ||
| 326 | my $pixbuf = $self->get_pixbuf; | ||
| 327 | my $ratio = $self->get_resolution_ratio; | ||
| 328 | my $viewport = $self->get_viewport; | ||
| 329 | $style->add_class('imageview'); | ||
| 330 | |||
| 331 | $style->save; | ||
| 332 | $style->add_class(Gtk3::STYLE_CLASS_BACKGROUND); | ||
| 333 | Gtk3::render_background( $style, $context, $allocation->{x}, | ||
| 334 | $allocation->{y}, $allocation->{width}, $allocation->{height} ); | ||
| 335 | $style->restore; | ||
| 336 | |||
| 337 | if ( defined $pixbuf ) { | ||
| 338 | if ( $pixbuf->get_has_alpha ) { | ||
| 339 | $style->save; | ||
| 340 | |||
| 341 | # '.imageview' affects also area outside of the image. But only | ||
| 342 | # when background-image is specified. background-color seems to | ||
| 343 | # have no effect there. Probably a bug in Gtk? Either way, this is | ||
| 344 | # why need a special class 'transparent' to match the correct area | ||
| 345 | # inside the image where both image and color work. | ||
| 346 | $style->add_class('transparent'); | ||
| 347 | my ( $x1, $y1 ) = $self->to_widget_coords( 0, 0 ); | ||
| 348 | my ( $x2, $y2 ) = | ||
| 349 | $self->to_widget_coords( $pixbuf->get_width, | ||
| 350 | $pixbuf->get_height ); | ||
| 351 | Gtk3::render_background( $style, $context, $x1, $y1, $x2 - $x1, | ||
| 352 | $y2 - $y1 ); | ||
| 353 | $style->restore; | ||
| 354 | } | ||
| 355 | |||
| 356 | my $zoom = $self->get_zoom / $self->get('scale-factor'); | ||
| 357 | $context->scale( $zoom / $ratio, $zoom ); | ||
| 358 | my $offset = $self->get_offset; | ||
| 359 | $context->translate( $offset->{x}, $offset->{y} ); | ||
| 360 | Gtk3::Gdk::cairo_set_source_pixbuf( $context, $pixbuf, 0, 0 ); | ||
| 361 | $context->get_source->set_filter( $self->get_interpolation ); | ||
| 362 | } | ||
| 363 | else { | ||
| 364 | my $bgcol = $style->get( 'normal', 'background-color' ); | ||
| 365 | Gtk3::Gdk::cairo_set_source_rgba( $context, $bgcol ); | ||
| 366 | } | ||
| 367 | $context->paint; | ||
| 368 | |||
| 369 | my $selection = $self->get_selection; | ||
| 370 | if ( defined $pixbuf and defined $selection ) { | ||
| 371 | my ( $x, $y, $w, $h, ) = ( | ||
| 372 | $selection->{x}, $selection->{y}, | ||
| 373 | $selection->{width}, $selection->{height}, | ||
| 374 | ); | ||
| 375 | if ( $w <= 0 or $h <= 0 ) { return TRUE } | ||
| 376 | |||
| 377 | $style->save; | ||
| 378 | $style->add_class(Gtk3::STYLE_CLASS_RUBBERBAND); | ||
| 379 | Gtk3::render_background( $style, $context, $x, $y, $w, $h ); | ||
| 380 | Gtk3::render_frame( $style, $context, $x, $y, $w, $h ); | ||
| 381 | $style->restore; | ||
| 382 | } | ||
| 383 | return TRUE; | ||
| 384 | } | ||
| 385 | |||
| 386 | sub _configure_event { | ||
| 387 | my ( $self, $event ) = @_; | ||
| 388 | if ( $self->get_zoom_to_fit ) { | ||
| 389 | $self->zoom_to_box( $self->get_pixbuf_size ); | ||
| 390 | } | ||
| 391 | return; | ||
| 392 | } | ||
| 393 | |||
| 394 | # setting the zoom via the public API disables zoom-to-fit | ||
| 395 | |||
| 396 | sub set_zoom { | ||
| 397 | my ( $self, $zoom ) = @_; | ||
| 398 | $self->set_zoom_to_fit(FALSE); | ||
| 399 | $self->_set_zoom_no_center($zoom); | ||
| 400 | return; | ||
| 401 | } | ||
| 402 | |||
| 403 | sub _set_zoom { | ||
| 404 | my ( $self, $zoom ) = @_; | ||
| 405 | if ( $zoom > $MAX_ZOOM ) { $zoom = $MAX_ZOOM } | ||
| 406 | $self->set( 'zoom', $zoom ); | ||
| 407 | return; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub get_zoom { | ||
| 411 | my ($self) = @_; | ||
| 412 | return $self->get('zoom'); | ||
| 413 | } | ||
| 414 | |||
| 415 | # convert x, y in image coords to widget coords | ||
| 416 | sub to_widget_coords { | ||
| 417 | my ( $self, $x, $y ) = @_; | ||
| 418 | my $zoom = $self->get_zoom; | ||
| 419 | my $ratio = $self->get_resolution_ratio; | ||
| 420 | my $offset = $self->get_offset; | ||
| 421 | my $factor = $self->get('scale-factor'); | ||
| 422 | return ( $x + $offset->{x} ) * $zoom / $factor / $ratio, | ||
| 423 | ( $y + $offset->{y} ) * $zoom / $factor; | ||
| 424 | } | ||
| 425 | |||
| 426 | # convert x, y in widget coords to image coords | ||
| 427 | sub to_image_coords { | ||
| 428 | my ( $self, $x, $y ) = @_; | ||
| 429 | my $zoom = $self->get_zoom; | ||
| 430 | my $ratio = $self->get_resolution_ratio; | ||
| 431 | my $offset = $self->get_offset; | ||
| 432 | my $factor = $self->get('scale-factor'); | ||
| 433 | return $x * $factor / $zoom * $ratio - $offset->{x}, | ||
| 434 | $y * $factor / $zoom - $offset->{y}; | ||
| 435 | } | ||
| 436 | |||
| 437 | # convert x, y in widget distance to image distance | ||
| 438 | sub to_image_distance { | ||
| 439 | my ( $self, $x, $y ) = @_; | ||
| 440 | my $zoom = $self->get_zoom; | ||
| 441 | my $ratio = $self->get_resolution_ratio; | ||
| 442 | my $factor = $self->get('scale-factor'); | ||
| 443 | return $x * $factor / $zoom * $ratio, $y * $factor / $zoom; | ||
| 444 | } | ||
| 445 | |||
| 446 | # set zoom with centre in image coordinates | ||
| 447 | sub _set_zoom_with_center { | ||
| 448 | my ( $self, $zoom, $center_x, $center_y ) = @_; | ||
| 449 | my $allocation = $self->get_allocation; | ||
| 450 | my $ratio = $self->get_resolution_ratio; | ||
| 451 | my $factor = $self->get('scale-factor'); | ||
| 452 | my $offset_x = | ||
| 453 | $allocation->{width} * $factor / 2 / $zoom * $ratio - $center_x; | ||
| 454 | my $offset_y = $allocation->{height} * $factor / 2 / $zoom - $center_y; | ||
| 455 | $self->_set_zoom($zoom); | ||
| 456 | $self->set_offset( $offset_x, $offset_y ); | ||
| 457 | return; | ||
| 458 | } | ||
| 459 | |||
| 460 | # sets zoom, centred on the viewport | ||
| 461 | sub _set_zoom_no_center { | ||
| 462 | my ( $self, $zoom ) = @_; | ||
| 463 | my $allocation = $self->get_allocation; | ||
| 464 | my ( $center_x, $center_y ) = | ||
| 465 | $self->to_image_coords( $allocation->{width} / 2, | ||
| 466 | $allocation->{height} / 2 ); | ||
| 467 | $self->_set_zoom_with_center( $zoom, $center_x, $center_y ); | ||
| 468 | return; | ||
| 469 | } | ||
| 470 | |||
| 471 | sub set_zoom_to_fit { | ||
| 472 | my ( $self, $zoom_to_fit, $limit ) = @_; | ||
| 473 | $self->set( 'zoom-to-fit', $zoom_to_fit ); | ||
| 474 | if ( defined $limit ) { | ||
| 475 | $self->set( 'zoom-to-fit-limit', $limit ); | ||
| 476 | } | ||
| 477 | if ( not $zoom_to_fit ) { return } | ||
| 478 | $self->zoom_to_box( $self->get_pixbuf_size ); | ||
| 479 | return; | ||
| 480 | } | ||
| 481 | |||
| 482 | sub zoom_to_box { | ||
| 483 | my ( $self, $box, $additional_factor ) = @_; | ||
| 484 | if ( not defined $box ) { return } | ||
| 485 | if ( not defined $box->{x} ) { $box->{x} = 0 } | ||
| 486 | if ( not defined $box->{y} ) { $box->{y} = 0 } | ||
| 487 | if ( not defined $additional_factor ) { $additional_factor = 1 } | ||
| 488 | my $allocation = $self->get_allocation; | ||
| 489 | my $ratio = $self->get_resolution_ratio; | ||
| 490 | my $limit = $self->get('zoom-to-fit-limit'); | ||
| 491 | my $sc_factor_w = | ||
| 492 | min( $limit, $allocation->{width} / $box->{width} ) * $ratio; | ||
| 493 | my $sc_factor_h = | ||
| 494 | min( $limit, $allocation->{height} / $box->{height} ); | ||
| 495 | $self->_set_zoom_with_center( | ||
| 496 | min( $sc_factor_w, $sc_factor_h ) * | ||
| 497 | $additional_factor * | ||
| 498 | $self->get('scale-factor'), | ||
| 499 | ( $box->{x} + $box->{width} / 2 ) / $ratio, | ||
| 500 | $box->{y} + $box->{height} / 2 | ||
| 501 | ); | ||
| 502 | return; | ||
| 503 | } | ||
| 504 | |||
| 505 | sub zoom_to_selection { | ||
| 506 | my ( $self, $context_factor ) = @_; | ||
| 507 | $self->zoom_to_box( $self->get_selection, $context_factor ); | ||
| 508 | return; | ||
| 509 | } | ||
| 510 | |||
| 511 | sub get_zoom_to_fit { | ||
| 512 | my ($self) = @_; | ||
| 513 | return $self->get('zoom-to-fit'); | ||
| 514 | } | ||
| 515 | |||
| 516 | sub zoom_in { | ||
| 517 | my ($self) = @_; | ||
| 518 | $self->set_zoom_to_fit(FALSE); | ||
| 519 | $self->_set_zoom_no_center( $self->get_zoom * $self->get('zoom-step') ); | ||
| 520 | return; | ||
| 521 | } | ||
| 522 | |||
| 523 | sub zoom_out { | ||
| 524 | my ($self) = @_; | ||
| 525 | $self->set_zoom_to_fit(FALSE); | ||
| 526 | $self->_set_zoom_no_center( $self->get_zoom / $self->get('zoom-step') ); | ||
| 527 | return; | ||
| 528 | } | ||
| 529 | |||
| 530 | sub zoom_to_fit { | ||
| 531 | my ($self) = @_; | ||
| 532 | $self->set_zoom_to_fit(TRUE); | ||
| 533 | return; | ||
| 534 | } | ||
| 535 | |||
| 536 | sub set_fitting { | ||
| 537 | my ( $self, $value ) = @_; | ||
| 538 | $self->set_zoom_to_fit( $value, 1 ); | ||
| 539 | return; | ||
| 540 | } | ||
| 541 | |||
| 542 | sub _clamp_direction { | ||
| 543 | my ( $offset, $allocation, $pixbuf_size ) = @_; | ||
| 544 | |||
| 545 | # Centre the image if it is smaller than the widget | ||
| 546 | if ( $allocation > $pixbuf_size ) { | ||
| 547 | $offset = ( $allocation - $pixbuf_size ) / 2; | ||
| 548 | } | ||
| 549 | |||
| 550 | # Otherwise don't allow the LH/top edge of the image to be visible | ||
| 551 | elsif ( $offset > 0 ) { | ||
| 552 | $offset = 0; | ||
| 553 | } | ||
| 554 | |||
| 555 | # Otherwise don't allow the RH/bottom edge of the image to be visible | ||
| 556 | elsif ( $offset < $allocation - $pixbuf_size ) { | ||
| 557 | $offset = $allocation - $pixbuf_size; | ||
| 558 | } | ||
| 559 | return $offset; | ||
| 560 | } | ||
| 561 | |||
| 562 | sub set_offset { | ||
| 563 | my ( $self, $offset_x, $offset_y ) = @_; | ||
| 564 | if ( not defined $self->get_pixbuf ) { return } | ||
| 565 | |||
| 566 | # Convert the widget size to image scale to make the comparisons easier | ||
| 567 | my $allocation = $self->get_allocation; | ||
| 568 | ( $allocation->{width}, $allocation->{height} ) = | ||
| 569 | $self->to_image_distance( $allocation->{width}, $allocation->{height} ); | ||
| 570 | my $pixbuf_size = $self->get_pixbuf_size; | ||
| 571 | |||
| 572 | $offset_x = _clamp_direction( $offset_x, $allocation->{width}, | ||
| 573 | $pixbuf_size->{width} ); | ||
| 574 | $offset_y = _clamp_direction( $offset_y, $allocation->{height}, | ||
| 575 | $pixbuf_size->{height} ); | ||
| 576 | |||
| 577 | $self->set( 'offset', { x => $offset_x, y => $offset_y } ); | ||
| 578 | return; | ||
| 579 | } | ||
| 580 | |||
| 581 | sub get_offset { | ||
| 582 | my ($self) = @_; | ||
| 583 | return $self->get('offset'); | ||
| 584 | } | ||
| 585 | |||
| 586 | sub get_viewport { | ||
| 587 | my ($self) = @_; | ||
| 588 | my $allocation = $self->get_allocation; | ||
| 589 | my $pixbuf = $self->get_pixbuf; | ||
| 590 | my ( $x, $y, $w, $h ); | ||
| 591 | if ( defined $pixbuf ) { | ||
| 592 | ( $x, $y, $w, $h ) = ( | ||
| 593 | $self->to_image_coords( 0, 0 ), | ||
| 594 | $self->to_image_distance( | ||
| 595 | $allocation->{width}, $allocation->{height} | ||
| 596 | ) | ||
| 597 | ); | ||
| 598 | } | ||
| 599 | else { | ||
| 600 | ( $x, $y, $w, $h ) = | ||
| 601 | ( 0, 0, $allocation->{width}, $allocation->{height} ); | ||
| 602 | } | ||
| 603 | return { x => $x, y => $y, width => $w, height => $h }; | ||
| 604 | } | ||
| 605 | |||
| 606 | sub set_tool { | ||
| 607 | my ( $self, $tool ) = @_; | ||
| 608 | if ( not( blessed $tool and $tool->isa('Gtk3::ImageView::Tool') ) ) { | ||
| 609 | |||
| 610 | # TODO remove this fallback, only accept Tool directly | ||
| 611 | if ( $tool eq 'dragger' ) { | ||
| 612 | $tool = Gtk3::ImageView::Tool::Dragger->new($self); | ||
| 613 | } | ||
| 614 | elsif ( $tool eq 'selector' ) { | ||
| 615 | $tool = Gtk3::ImageView::Tool::Selector->new($self); | ||
| 616 | } | ||
| 617 | else { | ||
| 618 | croak 'invalid set_tool call'; | ||
| 619 | } | ||
| 620 | } | ||
| 621 | $self->set( 'tool', $tool ); | ||
| 622 | return; | ||
| 623 | } | ||
| 624 | |||
| 625 | sub get_tool { | ||
| 626 | my ($self) = @_; | ||
| 627 | return $self->get('tool'); | ||
| 628 | } | ||
| 629 | |||
| 630 | sub set_selection { | ||
| 631 | my ( $self, $selection ) = @_; | ||
| 632 | my $pixbuf_size = $self->get_pixbuf_size; | ||
| 633 | if ( not defined $pixbuf_size ) { return } | ||
| 634 | if ( $selection->{x} < 0 ) { | ||
| 635 | $selection->{width} += $selection->{x}; | ||
| 636 | $selection->{x} = 0; | ||
| 637 | } | ||
| 638 | if ( $selection->{y} < 0 ) { | ||
| 639 | $selection->{height} += $selection->{y}; | ||
| 640 | $selection->{y} = 0; | ||
| 641 | } | ||
| 642 | if ( $selection->{x} + $selection->{width} > $pixbuf_size->{width} ) { | ||
| 643 | $selection->{width} = $pixbuf_size->{width} - $selection->{x}; | ||
| 644 | } | ||
| 645 | if ( $selection->{y} + $selection->{height} > $pixbuf_size->{height} ) { | ||
| 646 | $selection->{height} = $pixbuf_size->{height} - $selection->{y}; | ||
| 647 | } | ||
| 648 | $self->set( 'selection-float', $selection ); | ||
| 649 | return; | ||
| 650 | } | ||
| 651 | |||
| 652 | sub get_selection { | ||
| 653 | my ($self) = @_; | ||
| 654 | return $self->get('selection'); | ||
| 655 | } | ||
| 656 | |||
| 657 | sub set_resolution_ratio { | ||
| 658 | my ( $self, $ratio ) = @_; | ||
| 659 | $self->set( 'resolution-ratio', $ratio ); | ||
| 660 | if ( $self->get_zoom_to_fit ) { | ||
| 661 | $self->zoom_to_box( $self->get_pixbuf_size ); | ||
| 662 | } | ||
| 663 | return; | ||
| 664 | } | ||
| 665 | |||
| 666 | sub get_resolution_ratio { | ||
| 667 | my ($self) = @_; | ||
| 668 | return $self->get('resolution-ratio'); | ||
| 669 | } | ||
| 670 | |||
| 671 | sub update_cursor { | ||
| 672 | my ( $self, $x, $y ) = @_; | ||
| 673 | my $pixbuf_size = $self->get_pixbuf_size; | ||
| 674 | if ( not defined $pixbuf_size ) { return } | ||
| 675 | my $win = $self->get_window; | ||
| 676 | my $cursor = $self->get_tool->cursor_at_point( $x, $y ); | ||
| 677 | if ( defined $cursor ) { | ||
| 678 | $win->set_cursor($cursor); | ||
| 679 | } | ||
| 680 | |||
| 681 | return; | ||
| 682 | } | ||
| 683 | |||
| 684 | sub set_interpolation { | ||
| 685 | my ( $self, $interpolation ) = @_; | ||
| 686 | $self->set( 'interpolation', $interpolation ); | ||
| 687 | return; | ||
| 688 | } | ||
| 689 | |||
| 690 | sub get_interpolation { | ||
| 691 | my ($self) = @_; | ||
| 692 | return $self->get('interpolation'); | ||
| 693 | } | ||
| 694 | |||
| 695 | 1; | ||
| 696 | |||
| 697 | __END__ |